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

feat: add exclude_none parameter to model_dump_doc #503

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions odmantic/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,11 +709,17 @@ def __setattr__(self, name: str, value: Any) -> None:
@deprecated(
"doc is deprecated, please use model_dump_doc instead",
)
def doc(self, include: Optional["AbstractSetIntStr"] = None) -> Dict[str, Any]:
return self.model_dump_doc(include=include)
def doc(
self,
include: Optional["AbstractSetIntStr"] = None,
exclude_none: bool = False,
) -> Dict[str, Any]:
return self.model_dump_doc(include=include, exclude_none=exclude_none)

def model_dump_doc(
self, include: Optional["AbstractSetIntStr"] = None
self,
include: Optional["AbstractSetIntStr"] = None,
exclude_none: bool = False,
) -> Dict[str, Any]:
"""Generate a document (BSON) representation of the instance (as a dictionary).

Expand All @@ -724,7 +730,7 @@ def model_dump_doc(
Returns:
the document associated to the instance
"""
raw_doc = self.model_dump()
raw_doc = self.model_dump(exclude_none=exclude_none)
doc = self.__doc(raw_doc, type(self), include)
return doc

Expand All @@ -736,6 +742,8 @@ def __doc( # noqa C901 # TODO: refactor document generation
) -> Dict[str, Any]:
doc: Dict[str, Any] = {}
for field_name, field in model.__odm_fields__.items():
if field_name not in raw_doc:
continue
if include is not None and field_name not in include:
continue
if isinstance(field, ODMReference):
Expand Down