You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make Ormar compatible with Pydantic .construct() method or implement Ormar equivalent #318
Originally posted by franzwollang August 22, 2021
In Pydantic, one can use the .construct() method to instantiate a model while skipping validation. I tried it out with an Ormar model and it seemed to work, except there were some random exceptions when I tried to .save() it that gave me the impression that perhaps doing this broke some of Ormar's internals.
Does Ormar work with the .construct() method? Or is there an Ormar equivalent?
Is your feature request related to a problem? Please describe.
Pydantic has a class method that allows one to skip validation. Skipping validation can very useful for speeding up certain types of operations, like retrieving large amounts of data from a trusted source or using models to generate data for testing purposes. The Pydantic documentation claims the difference can be as much as 30X faster.
Describe the solution you'd like
For Ormar to be compatible with Pydantic's .construct() class method or to implement an Ormar native equivalent.
Describe alternatives you've considered
I vaguely recall there is something somewhere in the Ormar documentation that mentions validation can be skipped in a very specific situation, but I would have to very carefully comb through the documentation again and then see what the context of it was. I don't think it was intended as a general purpose option for skipping validation though.
I agree with many of the posters in the threads above, nested models is a very common use case; one of the core functionalities of an ORM is in the name, that of representing each table schema as a model and providing tools that make managing the models and their connections easier. So it would be very nice if it were supported here.
I don't think recursive models (or more generally cycles of models) need to be supported (i.e. there could be a stopping criterion when hitting the same model as the root model where the .construct() method was called), but nested models of different types definitely should be supported if possible. When there are recursive or cyclical structures, I think the programmer already needs to pay more attention and write extra code to handle their specific use cases, so it's not unreasonable to ask them to do a bit of extra work to figure out how to decompose and build their models in sections that are acyclic digraphs. But I think operations on acyclic digraph model structures is very common and would be a good performant baseline for reducing more complicated problems down into.
Ormar build a relation map which is kind of a tree that is checking reoccurring models on same paths and breaks there. It's used to load all/ save all/ dict/ json etc. So this one can be done with the same.
So including nested models basically means that i have to skip the original construct and overwrite it.
I guess it depends on what the "original construct" is. If you are just initializing Pydantic models (calling the models directly or using the __init__ method explicitly), then you would just have to substitute those calls for calls to the .construct() method on the models instead. That's a very simple substitution.
Activity
franzwollang commentedon Aug 23, 2021
Is your feature request related to a problem? Please describe.
Pydantic has a class method that allows one to skip validation. Skipping validation can very useful for speeding up certain types of operations, like retrieving large amounts of data from a trusted source or using models to generate data for testing purposes. The Pydantic documentation claims the difference can be as much as 30X faster.
Describe the solution you'd like
For Ormar to be compatible with Pydantic's .construct() class method or to implement an Ormar native equivalent.
Describe alternatives you've considered
I vaguely recall there is something somewhere in the Ormar documentation that mentions validation can be skipped in a very specific situation, but I would have to very carefully comb through the documentation again and then see what the context of it was. I don't think it was intended as a general purpose option for skipping validation though.
Additional context
Pydantic documentation:
https://pydantic-docs.helpmanual.io/usage/models/#creating-models-without-validation
Potentially helpful threads on the Pydantic repo related to this topic:
pydantic/pydantic#897
pydantic/pydantic#1168
pydantic/pydantic#1212
collerek commentedon Sep 8, 2021
Ok, I checked the links that you provided (thanks) and the code of
constructin the pydantic model.It seems that for the sake of speed this does not support nested models, how would you like it to work for
ormar?franzwollang commentedon Sep 13, 2021
Thanks for asking!
I agree with many of the posters in the threads above, nested models is a very common use case; one of the core functionalities of an ORM is in the name, that of representing each table schema as a model and providing tools that make managing the models and their connections easier. So it would be very nice if it were supported here.
I don't think recursive models (or more generally cycles of models) need to be supported (i.e. there could be a stopping criterion when hitting the same model as the root model where the .construct() method was called), but nested models of different types definitely should be supported if possible. When there are recursive or cyclical structures, I think the programmer already needs to pay more attention and write extra code to handle their specific use cases, so it's not unreasonable to ask them to do a bit of extra work to figure out how to decompose and build their models in sections that are acyclic digraphs. But I think operations on acyclic digraph model structures is very common and would be a good performant baseline for reducing more complicated problems down into.
collerek commentedon Sep 13, 2021
Ormar build a relation map which is kind of a tree that is checking reoccurring models on same paths and breaks there. It's used to load all/ save all/ dict/ json etc. So this one can be done with the same.
So including nested models basically means that i have to skip the original construct and overwrite it.
franzwollang commentedon Sep 15, 2021
I guess it depends on what the "original construct" is. If you are just initializing Pydantic models (calling the models directly or using the
__init__method explicitly), then you would just have to substitute those calls for calls to the .construct() method on the models instead. That's a very simple substitution.collerek commentedon Oct 13, 2021
Added/ Fixed in 0.10.21