django_sorcery.db.relations module¶
sqlalchemy relationship related things.
-
class
django_sorcery.db.relations.
RelationsMixin
[source]¶ Bases:
object
Mixin that provides django like shortcuts for relationships.
-
ManyToMany
(remote_cls, table_name=None, **kwargs)[source]¶ Use an event to build many-to-many relationship on a model and auto generates an association table or if a model is provided as secondary argument:
class ModelOne(db.Model): pk = db.Column(.., primary_key=True) m2s = db.ManyToMany("ModelTwo", backref="m1s", table_name='m1m2s', ...) class ModelTwo(db.Model): pk = db.Column(.., primary_key=True) ...
or with back_populates:
class ModelOne(db.Model): pk = db.Column(.., primary_key=True) m2s = db.ManyToMany("ModelTwo", back_populates="m1s", table_name='m1m2s', ...) class ModelTwo(db.Model): pk = db.Column(.., primary_key=True) m1s = db.ManyToMany("ModelOne", back_populates="m2s", table_name='m1m2s', ...)
will create ModelOne.m2s and ModelTwo.m1s relationship thru a provided secondary argument. If no secondary argument is provided, table_name is required as it will be used for the autogenerated association table.
In the case of back_populates you have to provide the same table_name argument on both many-to-many declarations
-
ManyToOne
(remote_cls, **kwargs)[source]¶ Use an event to build many-to-one relationship on a model and auto generates foreign key relationship on the remote table:
class ModelOne(db.Model): pk = db.Column(.., primary_key=True) m2 = db.ManyToOne("ModelTwo", ...) class ModelTwo(db.Model): pk = db.Column(.., primary_key=True) ...
will create ModelOne.m2_pk automatically for the relationship
-
OneToMany
(remote_cls, **kwargs)[source]¶ Use an event to build one-to-many relationship on a model and auto generates foreign key relationship from the remote table:
- class ModelOne(db.Model):
- pk = db.Column(.., primary_key=True) m2 = db.OneToMany(“ModelTwo”, …)
- class ModelTwo(db.Model):
- pk = db.Column(.., primary_key=True) …
will create ModelTwo.m1_pk automatically for the relationship
-
OneToOne
(remote_cls, **kwargs)[source]¶ Use an event to build one-to-many relationship on a model and auto generates foreign key relationship from the remote table:
- class ModelOne(db.Model):
- pk = db.Column(.., primary_key=True) m2 = db.OneToOne(“ModelTwo”, …)
- class ModelTwo(db.Model):
- pk = db.Column(.., primary_key=True) …
will create ModelTwo.m1_pk automatically for the relationship
-