One sided relations and more powerful save_related
0.10.3
✨ Features
-
ForeignKey
andManyToMany
now supportskip_reverse: bool = False
flag #118.
If you setskip_reverse
flag internally the field is still registered on the other
side of the relationship so you can:filter
by related models fields from reverse modelorder_by
by related models fields from reverse model
But you cannot:
- access the related field from reverse model with
related_name
- even if you
select_related
from reverse side of the model the returned models won't be populated in reversed instance (the join is not prevented so you still canfilter
andorder_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 onextra
setting in pydanticConfig
.
-
Model.save_related()
now can save whole data tree in once #148
meaning:-
it knows if it should save main
Model
or relatedModel
first to preserve the relation -
it saves main
Model
if- it's not
saved
, - has no
pk
value - or
save_all=True
flag is set
in those cases you don't have to split save into two calls (
save()
andsave_related()
) - it's not
-
it supports also
ManyToMany
relations -
it supports also optional
Through
model values for m2m relations
-
-
Add possibility to customize
Through
model relation field names. -
By default
Through
model 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 whichManyToMany
is declaredthrough_reverse_relation_name
- name of the field leading to the model to whichManyToMany
leads 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