admin管理员组文章数量:1291122
I would like to define my models using pyndatic
such that the validation/serialization into mongodb
documents is done without losing information.
In particular, assuming I wish to replicate this base example with a nested serialized model:
{
"_id": "joe",
"name": "Joe Bookreader",
"addresses": [
{
"street": "123 Fake Street",
"city": "Faketon",
"state": "MA",
"zip": "12345"
},
{
"street": "1 Some Other Street",
"city": "Boston",
"state": "MA",
"zip": "12345"
}
]
}
Reference: denormalized nested model
How should I define the Pydantic models so that the instances of the sub-model Address
, once validated by creation or update (by changing the config on updates) actually contains the fields id
and name
?
I tried using some field or model validators but I figured out that if the fields are required, then they must exists when internal validation is performed to create the model and my after validator fails at runtime. I could not inject the dependency this way. The before validators seems too complex for my use case (I have a very complex nested model where properties are exploited between sub-models to implements computed fields and other logics.
If possible I would like the fields to be required, in order to have them in runtime, but not serialized when dumping the models in order to get the provided example ready to be saved onto the mongodb.
Is there an easy and smart design solution to the problem?
版权声明:本文标题:dependency injection - Model One-to-Many Relationships with Embedded Documents using Pydantic - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741513075a2382722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论