Skip to content

Conversation

@d3cryptofc
Copy link

@d3cryptofc d3cryptofc commented Oct 27, 2024

This PR closes #504 issue.

Model example:

from odmantic import Field, Model, EmbeddedModel
from typing import Optional

class Mother(EmbeddedModel):
   name: Optional[str] = None

class Person(Model):
   name: Optional[str] = None
   mother: Mother = None

Demo:

In [1]: p = Person(mother=Mother())

In [2]: p.model_dump_doc()
Out[2]: 
{'name': None,
 'mother': {'name': None},
 '_id': ObjectId('671e9987bab71c701c80d08f')}

In [3]: p.model_dump_doc(exclude_none=True)
Out[3]: {'mother': {}, '_id': ObjectId('671e9987bab71c701c80d08f')}

Although in this example there is still an empty dictionary left, removing the key from it would also be good, but it distances itself from the exclude_none parameter.

Maybe an exclude_by_func parameter would be ideal?

This way, it is up to the user to choose the types of data (or even data) that should be excluded, however, it implies adding this feature to pydantic so that we can use it, or recreating model_dump from scratch.

I created an issue on pydantic about this, I hope the idea is approved: pydantic/pydantic#10728

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

Successfully merging this pull request may close these issues.

Dump an model excluding none values

1 participant