Skip to content

Add through fields, load_all() method and make through models optional

Compare
Choose a tag to compare
@collerek collerek released this 05 Mar 11:48
· 954 commits to master since this release
7c0f8e9

0.9.6

Important

  • Through model for ManyToMany relations now becomes optional. It's not a breaking change
    since if you provide it everything works just fine as it used to. So if you don't want or need any additional
    fields on Through model you can skip it. Note that it's going to be created for you automatically and
    still has to be included in example in alembic migrations.
    If you want to delete existing one check the default naming convention to adjust your existing database structure.

    Note that you still need to provide it if you want to
    customize the Through model name or the database table name.

Features

  • Add update method to QuerysetProxy so now it's possible to update related models directly from parent model
    in ManyToMany relations and in reverse ForeignKey relations. Note that update like in QuerySet update returns number of
    updated models and does not update related models in place on parent model. To get the refreshed data on parent model you need to refresh
    the related models (i.e. await model_instance.related.all())
  • Add load_all(follow=False, exclude=None) model method that allows to load current instance of the model
    with all related models in one call. By default it loads only directly related models but setting
    follow=True causes traversing the tree (avoiding loops). You can also pass exclude parameter
    that works the same as QuerySet.exclude_fields() method.
  • Added possibility to add more fields on Through model for ManyToMany relationships:
    • name of the through model field is the lowercase name of the Through class
    • you can pass additional fields when calling add(child, **kwargs) on relation (on QuerysetProxy)
    • you can pass additional fields when calling create(**kwargs) on relation (on QuerysetProxy)
      when one of the keyword arguments should be the through model name with a dict of values
    • you can order by on through model fields
    • you can filter on through model fields
    • you can include and exclude fields on through models
    • through models are attached only to related models (i.e. if you query from A to B -> only on B)
    • note that through models are explicitly loaded without relations -> relation is already populated in ManyToMany field.
    • note that just like before you cannot declare the relation fields on through model, they will be populated for you by ormar,
      but now if you try to do so ModelDefinitionError will be thrown
    • check the updated ManyToMany relation docs for more information

Other

  • Updated docs and api docs
  • Refactors and optimisations mainly related to filters, exclusions and order bys