Releases: ormar-orm/ormar
Releases · ormar-orm/ormar
Add default ordering, new aggr functions, new signals
0.9.9
Features
- Add possibility to change default ordering of relations and models.
- To change model sorting pass
orders_by = [columns]wherecolumns: List[str]to modelMetaclass - To change relation order_by pass
orders_by = [columns]wherecolumns: List[str] - To change reverse relation order_by pass
related_orders_by = [columns]wherecolumns: 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 forManyToManycolumns alsoThroughmodel 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_byif exists - Model
Metaclassorders_by - Model primary key column asc (fallback, used if none of above provided)
- Explicitly passed
- To change model sorting pass
- Add 4 new aggregated functions ->
min,max,sumandavgthat 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 havesum(col1, col2)and later add 2 returned sums in python) - You cannot
sumandavgnon 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_removeandpost_relation_remove- The newly added signals are emitted for
ManyToManyrelations (both sides)
and reverse side ofForeignKeyrelation (same asQuerysetProxyis 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 alsopassed_kwargs: Dict- dict of kwargs passed toadd()
- The newly added signals are emitted for
Changes
Throughmodels 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
Throughmodels are now instantiated you can also subscribe toThroughmodel
pre/post save/update/delete signals pre_updatesignals receivers now get also passed_args argument which is a
dict of values passed to update function if any (else empty dict)
Fixes
pre_updatesignal 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 databasebulk_updatenow works correctly also withUUIDprimary key column type
Fields encryption
0.9.8
Features
- Add possibility to encrypt the selected field(s) in the database
- As minimum you need to provide
encrypt_secretandencrypt_backend encrypt_backendcan be one of theormar.EncryptBackendsenum (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_keycolumn 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
filteringpossibility altogether as part of the encrypted value is a timestamp. - Note that in HASH backend you can filter by full value but filters like
containwill not work as comparison is make on encrypted values - Note that adding
encrypt_backendchanges the database column type toTEXT, which needs to be reflected in db either by migration or manual change
- As minimum you need to provide
Fixes
- (Advanced/ Internal) Restore custom sqlalchemy types (by
types.TypeDecoratorsubclass) functionality that ceased to working soprocess_result_valuewas never called
Isnull filter and complex filters (including or)
0.9.7
Features
- Add
isnulloperator 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_andormar.and_functions that can be used to compose
complex queries with nested conditions.
Sample query:Check the updated docs in Queries -> Filtering and sorting -> Complex filtersbooks = ( await Book.objects.select_related("author") .filter( ormar.and_( ormar.or_(year__gt=1960, year__lt=1940), author__name="J.R.R. Tolkien", ) ) .all() )
Other
- Setting default on
ForeignKeyorManyToManyraises andModelDefinitionexception as it is (and was) not supported
Add through fields, load_all() method and make through models optional
0.9.6
Important
-
Throughmodel forManyToManyrelations 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 onThroughmodel you can skip it. Note that it's going to be created for you automatically and
still has to be included in example inalembicmigrations.
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 theThroughmodel name or the database table name.
Features
- Add
updatemethod toQuerysetProxyso now it's possible to update related models directly from parent model
inManyToManyrelations and in reverseForeignKeyrelations. Note that update like inQuerySetupdatereturns 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=Truecauses traversing the tree (avoiding loops). You can also passexcludeparameter
that works the same asQuerySet.exclude_fields()method. - Added possibility to add more fields on
Throughmodel forManyToManyrelationships:- 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 (onQuerysetProxy) - you can pass additional fields when calling
create(**kwargs)on relation (onQuerysetProxy)
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 soModelDefinitionErrorwill 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
0.9.5
Fixes
- Fix creation of
pydanticFieldInfo after update ofpydanticto 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
0.9.4
Fixes
- Fix
fastapiOpenAPI schema generation for automatic docs when multiple models refer to the same related one
Fix Json fields and choices validation
Fixes
- Fix
JSONfield being double escaped when setting value after initialization - Fix
JSONfield not respectingnullablefield setting due topydanticinternals - Fix
choicesverification forJSONfield - Fix
choicesnot being verified when setting the attribute after initialization - Fix
choicesnot being verified duringupdatecall fromQuerySet
Update docs and quick start so publish on pypi
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
0.9.1
Features
- Add choices values to
OpenAPIspecs, so it looks like nativeEnumfield in the result schema.
Fixes
- Fix
choicesbehavior withfastapiusage when special fields can be not initialized yet but passed as strings etc.
Fix for foreign keys missing in generated ddl for all backends
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 theormarfunctions 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
ormarto 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
Metaclass of theModeldoes not
includemetadataordatabasenowModelDefinitionErrorwill be raised instead of genericAttributeError. - Breaking:
encode/databasesused 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 inormarsince >=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