odmantic
odmantic copied to clipboard
feat: add `exclude_none` parameter to `model_dump_doc`
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: https://github.com/pydantic/pydantic/issues/10728