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

Allow exclude_none, exclude_unset, exclude_defaults on engine.save() #162

Open
hellocoldworld opened this issue Jul 23, 2021 · 0 comments · May be fixed by #180
Open

Allow exclude_none, exclude_unset, exclude_defaults on engine.save() #162

hellocoldworld opened this issue Jul 23, 2021 · 0 comments · May be fixed by #180
Labels
enhancement New feature or request

Comments

@hellocoldworld
Copy link

Feature request

Context

I need to save ODMantic documents, but almost always I need to save them in a way that unset fields are not persisted in mongoDB. This is mainly because unsetted fields should not be included in unique, sparse indexes. Also I can see how exclude_none and exclude_defaults can be useful.

Solution

I think that engine.save should allow exclude_none, exclude_unset and exclude_defaults as keyword arguments in its. public API, just like Pydantic.BaseModel.dict. I would expect that when passing any of this excluding argument, the fields that are none/unset/default value would not be persisted in the DB.

Alternative solutions

Currently I am working with this approach:

        doc = request.dict(exclude_unset=True)
         id_ = await engine.get_collection(self.model).insert_one(doc) 
        result = await engine.find_one(self.model, self.model.id == id_.inserted_id)

but I think it's ugly and should be probably handled better in ODMantic right away.

What do you think of this idea? I could perhaps help with a PR with some guidance.

@hellocoldworld hellocoldworld added the enhancement New feature or request label Jul 23, 2021
hellocoldworld pushed a commit to hellocoldworld/odmantic that referenced this issue Sep 7, 2021
Allow passing exclude keyword arguments to engine.save.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant