From 10e2d01a9108c072a29fcd0d2caf654c7c462bd2 Mon Sep 17 00:00:00 2001 From: collerek Date: Tue, 12 Oct 2021 18:52:40 +0200 Subject: [PATCH] update docs, bump version, update releases --- docs/models/methods.md | 27 ++++++++++++++++++- docs/releases.md | 7 +++++ pyproject.toml | 2 +- .../test_validators_in_generated_pydantic.py | 6 ----- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/docs/models/methods.md b/docs/models/methods.md index 0761a29..7cbecea 100644 --- a/docs/models/methods.md +++ b/docs/models/methods.md @@ -17,6 +17,25 @@ especially `dict()` and `json()` methods that can also accept `exclude`, `includ To read more check [pydantic][pydantic] documentation +## construct + +`construct` is a raw equivalent of `__init__` method used for construction of new instances. + +The difference is that `construct` skips validations, so it should be used when you know that data is correct and can be trusted. +The benefit of using construct is the speed of execution due to skipped validation. + +!!!note + Note that in contrast to `pydantic.construct` method - the `ormar` equivalent will also process the nested related models. + +!!!warning + Bear in mind that due to skipped validation the `construct` method does not perform any conversions, checks etc. + So it's your responsibility to provide tha data that is valid and can be consumed by the database. + + The only two things that construct still performs are: + + * Providing a `default` value for not set fields + * Initialize nested ormar models if you pass a dictionary or a primary key value + ## dict `dict` is a method inherited from `pydantic`, yet `ormar` adds its own parameters and has some nuances when working with default values, @@ -363,10 +382,16 @@ class Category(BaseModel): items: Optional[List[Item]] ``` -Of course you can use also deeply nested structures and ormar will generate it pydantic equivalent you (in a way that exclude loops). +Of course, you can use also deeply nested structures and ormar will generate it pydantic equivalent you (in a way that exclude loops). Note how `Item` model above does not have a reference to `Category` although in ormar the relation is bidirectional (and `ormar.Item` has `categories` field). +!!!warning + Note that the generated pydantic model will inherit all **field** validators from the original `ormar` model, that includes the ormar choices validator as well as validators defined with `pydantic.validator` decorator. + + But, at the same time all root validators present on `ormar` models will **NOT** be copied to the generated pydantic model. Since root validator can operate on all fields and a user can exclude some fields during generation of pydantic model it's not safe to copy those validators. + If required, you need to redefine/ manually copy them to generated pydantic model. + ## load By default when you query a table without prefetching related models, the ormar will still construct diff --git a/docs/releases.md b/docs/releases.md index babf6e6..e2b11bf 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -1,3 +1,10 @@ +# 0.10.21 + +## 🐛 Fixes + +* Add `ormar` implementation of `construct` classmethod that allows to build `Model` instances without validating the input to speed up the whole flow, if your data is already validated [#318](https://github.com/collerek/ormar/issues/318) +* Fix for "inheriting" field validators from `ormar` model when newly created pydanic model is generated with `get_pydantic` [#365](https://github.com/collerek/ormar/issues/365) + # 0.10.20 ## ✨ Features diff --git a/pyproject.toml b/pyproject.toml index 1b71ed1..3b8f9d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ormar" -version = "0.10.20" +version = "0.10.21" description = "A simple async ORM with fastapi in mind and pydantic validation." authors = ["Radosław Drążkiewicz "] license = "MIT" diff --git a/tests/test_inheritance_and_pydantic_generation/test_validators_in_generated_pydantic.py b/tests/test_inheritance_and_pydantic_generation/test_validators_in_generated_pydantic.py index 270d14e..d105481 100644 --- a/tests/test_inheritance_and_pydantic_generation/test_validators_in_generated_pydantic.py +++ b/tests/test_inheritance_and_pydantic_generation/test_validators_in_generated_pydantic.py @@ -43,12 +43,6 @@ class ModelExample(ormar.Model): raise ValueError("must contain a space") return v - @pydantic.validator("str_field") - def validate_str_field2(cls, v): - if " " not in v: - raise ValueError("must contain a space") - return v - ModelExampleCreate = ModelExample.get_pydantic(exclude={"id"})