beanie
beanie copied to clipboard
[BUG] pydantic computed properties omitted during `insert_many` operation on Document
Describe the bug
Pydantic V2 have a bug where __iter__
does not include computed property. https://github.com/pydantic/pydantic/issues/8564
This cause the document to omit the computed properties during insert, as Encoder
is using __iter__
to get all properties.
To Reproduce
from beanie import Document
class TestModel(Document):
normal: int
@pydantic.computed_field
@property
def computed(self) -> int:
return 1
instance = TestModel(normal=42)
assert {field: value for field, value in instance} == instance.model_dump() # fails
# or
TestModel.insert_many([instance]) # This document in mongo will omit `computed` property.
Expected behavior Expect all properties to be included during the insert operation.
Additional context
As this issue is still open on pydantic
, not sure if we need to wait for a fix from pydantic
.