Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to validate a document when specifying a reference #498

Open
theroucken opened this issue Sep 15, 2024 · 0 comments
Open

Unable to validate a document when specifying a reference #498

theroucken opened this issue Sep 15, 2024 · 0 comments

Comments

@theroucken
Copy link

In one of the models, I create a reference to another model. When I try to validate the received document using “model_validate_doc”, I get the error “AttributeError: ‘ObjectId’ object has no attribute ‘get’”.

>     Customer.model_validate_doc(customer.model_dump_doc())
>   File ".venv\Lib\site-packages\odmantic\model.py", line 795, in model_validate_doc
>     errors, obj = cls._parse_doc_to_obj(raw_doc)
>                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   File ".venv\Lib\site-packages\odmantic\model.py", line 830, in _parse_doc_to_obj
>     sub_errors, sub_obj = field.model._parse_doc_to_obj(
>                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   File ".venv\Lib\site-packages\odmantic\model.py", line 910, in _parse_doc_to_obj
>     value = raw_doc.get(field.key_name, Undefined)
>             ^^^^^^^^^^^
> AttributeError: 'ObjectId' object has no attribute 'get'
class Shop(Model):
    ...

class Customer(Model):
    user_id: int
    shop: Shop = Reference()

...

shop = await engine.find_one(Shop)
customer = await engine.find_one(Customer, Customer.shop == shop.id)
Customer.model_validate_doc(customer.model_dump_doc())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant