Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One sided relations and more powerful save_related #150

Merged
merged 6 commits into from
Apr 16, 2021
Merged

Conversation

collerek
Copy link
Owner

@collerek collerek commented Apr 12, 2021

0.10.3

✨ Features

  • ForeignKey and ManyToMany now support skip_reverse: bool = False flag #118.
    If you set skip_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 model
    • order_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 can filter and order_by)
    • the relation won't be populated in dict() and json()
    • you cannot pass the nested related objects when populating from dict() or json() (also through fastapi). It will be either ignored or raise error depending on extra setting in pydantic Config.
  • Model.save_related() now can save whole data tree in once #148
    meaning:

    • it knows if it should save main Model or related Model 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() and save_related())

    • 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 which ManyToMany is declared
    • through_reverse_relation_name - name of the field leading to the model to which ManyToMany 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

@codecov-io
Copy link

codecov-io commented Apr 16, 2021

Codecov Report

Merging #150 (15e12ef) into master (e553885) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##            master      #150    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files          134       140     +6     
  Lines        11488     12130   +642     
==========================================
+ Hits         11488     12130   +642     
Impacted Files Coverage Δ
ormar/models/helpers/relations.py 100.00% <ø> (ø)
ormar/fields/base.py 100.00% <100.00%> (ø)
ormar/fields/foreign_key.py 100.00% <100.00%> (ø)
ormar/fields/many_to_many.py 100.00% <100.00%> (ø)
ormar/models/metaclass.py 100.00% <100.00%> (ø)
ormar/models/mixins/excludable_mixin.py 100.00% <100.00%> (ø)
ormar/models/mixins/relation_mixin.py 100.00% <100.00%> (ø)
ormar/models/mixins/save_mixin.py 100.00% <100.00%> (ø)
ormar/models/model.py 100.00% <100.00%> (ø)
ormar/models/newbasemodel.py 100.00% <100.00%> (ø)
... and 17 more

@collerek collerek merged commit fa79240 into master Apr 16, 2021
@collerek collerek deleted the relations_params branch April 16, 2021 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants