One sided relations and more powerful save_related
0.10.3
✨ Features
-
ForeignKeyandManyToManynow supportskip_reverse: bool = Falseflag #118.
If you setskip_reverseflag internally the field is still registered on the other
side of the relationship so you can:filterby related models fields from reverse modelorder_byby related models fields from reverse model
But you cannot:
- access the related field from reverse model with
related_name - even if you
select_relatedfrom reverse side of the model the returned models won't be populated in reversed instance (the join is not prevented so you still canfilterandorder_by) - the relation won't be populated in
dict()andjson() - you cannot pass the nested related objects when populating from
dict()orjson()(also throughfastapi). It will be either ignored or raise error depending onextrasetting in pydanticConfig.
-
Model.save_related()now can save whole data tree in once #148
meaning:-
it knows if it should save main
Modelor relatedModelfirst to preserve the relation -
it saves main
Modelif- it's not
saved, - has no
pkvalue - or
save_all=Trueflag is set
in those cases you don't have to split save into two calls (
save()andsave_related()) - it's not
-
it supports also
ManyToManyrelations -
it supports also optional
Throughmodel values for m2m relations
-
-
Add possibility to customize
Throughmodel relation field names. -
By default
Throughmodel relation names default to related model name in lowercase.
So in example like this:... # course declaration ommited class Student(ormar.Model): class Meta: database = database metadata = metadata id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) courses = ormar.ManyToMany(Course) # will produce default Through model like follows (example simplified) class StudentCourse(ormar.Model): class Meta: database = database metadata = metadata tablename = "students_courses" id: int = ormar.Integer(primary_key=True) student = ormar.ForeignKey(Student) # default name course = ormar.ForeignKey(Course) # default name
-
To customize the names of fields/relation in Through model now you can use new parameters to
ManyToMany:through_relation_name- name of the field leading to the model in whichManyToManyis declaredthrough_reverse_relation_name- name of the field leading to the model to whichManyToManyleads to
Example:
... # course declaration ommited class Student(ormar.Model): class Meta: database = database metadata = metadata id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) courses = ormar.ManyToMany(Course, through_relation_name="student_id", through_reverse_relation_name="course_id") # will produce default Through model like follows (example simplified) class StudentCourse(ormar.Model): class Meta: database = database metadata = metadata tablename = "students_courses" id: int = ormar.Integer(primary_key=True) student_id = ormar.ForeignKey(Student) # set by through_relation_name course_id = ormar.ForeignKey(Course) # set by through_reverse_relation_name