Skip to content

Releases: ormar-orm/ormar

Add default ordering, new aggr functions, new signals

15 Mar 18:01
61c456a

Choose a tag to compare

0.9.9

Features

  • Add possibility to change default ordering of relations and models.
    • To change model sorting pass orders_by = [columns] where columns: List[str] to model Meta class
    • To change relation order_by pass orders_by = [columns] where columns: List[str]
    • To change reverse relation order_by pass related_orders_by = [columns] where columns: List[str]
    • Arguments can be column names or -{col_name} to sort descending
    • In relations you can sort only by directly related model columns
      or for ManyToMany columns also Through model columns "{through_field_name}__{column_name}"
    • Order in which order_by clauses are applied is as follows:
      • Explicitly passed order_by() calls in query
      • Relation passed orders_by if exists
      • Model Meta class orders_by
      • Model primary key column asc (fallback, used if none of above provided)
  • Add 4 new aggregated functions -> min, max, sum and avg that are their
    corresponding sql equivalents.
    • You can pass one or many column names including related columns.
    • As of now each column passed is aggregated separately (so sum(col1+col2) is not possible,
      you can have sum(col1, col2) and later add 2 returned sums in python)
    • You cannot sum and avg non numeric columns
    • If you aggregate on one column, the single value is directly returned as a result
    • If you aggregate on multiple columns a dictionary with column: result pairs is returned
  • Add 4 new signals -> pre_relation_add, post_relation_add, pre_relation_remove and post_relation_remove
    • The newly added signals are emitted for ManyToMany relations (both sides)
      and reverse side of ForeignKey relation (same as QuerysetProxy is exposed).
    • Signals recieve following args: sender: Type[Model] - sender class,
      instance: Model - instance to which related model is added, child: Model - model being added,
      relation_name: str - name of the relation to which child is added,
      for add signals also passed_kwargs: Dict - dict of kwargs passed to add()

Changes

  • Through models for ManyToMany relations are now instantiated on creation, deletion and update, so you can provide not only
    autoincrement int as a primary key but any column type with default function provided.
  • Since Through models are now instantiated you can also subscribe to Through model
    pre/post save/update/delete signals
  • pre_update signals receivers now get also passed_args argument which is a
    dict of values passed to update function if any (else empty dict)

Fixes

  • pre_update signal now is sent before the extraction of values so you can modify the passed
    instance in place and modified fields values will be reflected in database
  • bulk_update now works correctly also with UUID primary key column type

Fields encryption

10 Mar 13:15
e306eec

Choose a tag to compare

0.9.8

Features

  • Add possibility to encrypt the selected field(s) in the database
    • As minimum you need to provide encrypt_secret and encrypt_backend
    • encrypt_backend can be one of the ormar.EncryptBackends enum (NONE, FERNET, HASH, CUSTOM) - default: NONE
    • When custom backend is selected you need to provide your backend class that subclasses ormar.fields.EncryptBackend
    • You cannot encrypt primary_key column and relation columns (FK and M2M).
    • Provided are 2 backends: HASH and FERNET
      • HASH is a one-way hash (like for password), never decrypted on retrieval
      • FERNET is a two-way encrypt/decrypt backend
    • Note that in FERNET backend you loose filtering possibility altogether as part of the encrypted value is a timestamp.
    • Note that in HASH backend you can filter by full value but filters like contain will not work as comparison is make on encrypted values
    • Note that adding encrypt_backend changes the database column type to TEXT, which needs to be reflected in db either by migration or manual change

Fixes

  • (Advanced/ Internal) Restore custom sqlalchemy types (by types.TypeDecorator subclass) functionality that ceased to working so process_result_value was never called

Isnull filter and complex filters (including or)

09 Mar 09:32
d7931a2

Choose a tag to compare

0.9.7

Features

  • Add isnull operator to filter and exclude methods.
    album__name__isnull=True #(sql: album.name is null)
    album__name__isnull=False #(sql: album.name is not null))
  • Add ormar.or_ and ormar.and_ functions that can be used to compose
    complex queries with nested conditions.
    Sample query:
    books = (
        await Book.objects.select_related("author")
        .filter(
            ormar.and_(
                ormar.or_(year__gt=1960, year__lt=1940),
                author__name="J.R.R. Tolkien",
            )
        )
        .all()
    )
    Check the updated docs in Queries -> Filtering and sorting -> Complex filters

Other

  • Setting default on ForeignKey or ManyToMany raises and ModelDefinition exception as it is (and was) not supported

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

05 Mar 11:48
7c0f8e9

Choose a tag to compare

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

Fix pydantic update

28 Feb 08:26

Choose a tag to compare

0.9.5

Fixes

  • Fix creation of pydantic FieldInfo after update of pydantic to version >=1.8
  • Pin required dependency versions to avoid such situations in the future

Fix `fastapi` OpenAPI schema generation for automatic docs when multiple models refer to the same related one

17 Feb 17:28
88baf9e

Choose a tag to compare

0.9.4

Fixes

  • Fix fastapi OpenAPI schema generation for automatic docs when multiple models refer to the same related one

Fix Json fields and choices validation

11 Feb 10:44
56d8c11

Choose a tag to compare

Fixes

  • Fix JSON field being double escaped when setting value after initialization
  • Fix JSON field not respecting nullable field setting due to pydantic internals
  • Fix choices verification for JSON field
  • Fix choices not being verified when setting the attribute after initialization
  • Fix choices not being verified during update call from QuerySet

Update docs and quick start so publish on pypi

06 Feb 13:24

Choose a tag to compare

Other

  • Updated the Quick Start in docs/readme
  • Updated docs with links to queries subpage
  • Added badges for code climate and pepy downloads

Add choices params as Enum details

03 Feb 14:04
16cd068

Choose a tag to compare

0.9.1

Features

  • Add choices values to OpenAPI specs, so it looks like native Enum field in the result schema.

Fixes

  • Fix choices behavior with fastapi usage when special fields can be not initialized yet but passed as strings etc.

Fix for foreign keys missing in generated ddl for all backends

02 Feb 11:54
a6166ed

Choose a tag to compare

0.9.0

Important

  • Braking Fix: Version 0.8.0 introduced a bug that prevents generation of foreign_keys constraint in the database,
    both in alembic and during creation through sqlalchemy.engine, this is fixed now.
  • THEREFORE IF YOU USE VERSION >=0.8.0 YOU ARE STRONGLY ADVISED TO UPDATE cause despite
    that most of the ormar functions are working your database CREATED with ormar (or ormar + alembic)
    does not have relations and suffer from perspective of performance and data integrity.
  • If you were using ormar to connect to existing database your performance and integrity
    should be fine nevertheless you should update to reflect all future schema updates in your models.

Breaking

  • Breaking: All foreign_keys and unique constraints now have a name so alembic
    can identify them in db and not depend on db
  • Breaking: During model construction if Meta class of the Model does not
    include metadata or database now ModelDefinitionError will be raised instead of generic AttributeError.
  • Breaking: encode/databases used for running the queries does not have a connection pool
    for sqlite backend, meaning that each querry is run with a new connection and there is no way to
    enable enforcing ForeignKeys constraints as those are by default turned off on every connection.
    This is changed in ormar since >=0.9.0 and by default each sqlite3 query has "PRAGMA foreign_keys=1;"
    run so now each sqlite3 connection by default enforces ForeignKey constraints including cascades.

Other

  • Update api docs.
  • Add tests for fk creation in db and for cascades in db