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

[BUG] Exclude Parameter on Model Dump Does Not Work If Lazy Parsing With Embedded Pydantic Models #967

Open
ltieman opened this issue Jul 9, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@ltieman
Copy link

ltieman commented Jul 9, 2024

Describe the bug
A clear and concise description of what the bug is.

.model_dump tries to call the .model_dump method of a pydantic model which is embedded inside a Beanie Document, which is not yet an actual pydantic model, but instead a NotAnObject. As a workaround, we are referencing the parameter which forces the data to be parsed.

Interestingly, we do not see this behavior with the include parameter on .model_dump, and instead it works as expected.

To Reproduce

class SubCollection(BaseModel):
    something: str
    
class Collection(Document):
    name: str
    sub: SubCollection

item = await Collection.find_one(Collection.id == id, fetch_links=True, lazy_parse=True)
return item.model_dump(exclude={"name":True})

Exception:

pydantic_core._pydantic_core.PydanticSerializationError: Error calling function `<lambda>`: AttributeError: 'NotAnObject' object has no attribute 'model_dump'

This, however, works:

class SubCollection(BaseModel):
    something: str
    
class Collection(Document):
    name: str
    sub: SubCollection

item = await Collection.find_one(Collection.id == id, fetch_links=True, lazy_parse=True)
item.sub
return item.model_dump(exclude={"name":True})

Expected behavior
We are expecting it to return a dictionary with all the keys except for "name"

Copy link
Contributor

github-actions bot commented Aug 9, 2024

This issue is stale because it has been open 30 days with no activity.

@github-actions github-actions bot added the Stale label Aug 9, 2024
@adeelsohailahmed adeelsohailahmed added bug Something isn't working and removed Stale labels Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants