-
Notifications
You must be signed in to change notification settings - Fork 62
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
Set a field to None removes it from DB #23
Conversation
The use of The policy in umongo is to say "None is None", so if you want to set a field to If you want to delete a field from a document, then you should use the Only exception is the fields that are missing in the database which are represented to the user as I realize this behavior can cause some trouble, so maybe we should add an special message when the user try to set a field to |
We've been discussing this today.
OK, this make sense.
Wouldn't it be better to raise We see two reasons for this:
I hope I'm making sense. In fact, consistency was the reason I created this PR, but I did it the other way around, which was wrong. If you agree with this, I'd like to close this PR and send a new one making def get(self, name, to_raise=KeyError):
if name not in self._fields:
raise to_raise(name)
field = self._fields[name]
name = field.attribute or name
value = self._data[name]
if value is missing:
if self.partial:
raise FieldNotLoadedError(name)
elif field.default is not missing:
return field.default
else:
return to_raise
return value Doing this, we keep the default value. That is, assuming I have a field with a default value: del my_doc.my_attribute
# This prints default_value (no exception)
print(my_doc.my_attribute) I don't really understand the use of |
On second thought, the "consistency" argument is based on the wrong assumption that when deleting an attribute from a doc would yield an Considering the difference between I don't have any definitive opinion about this, but in any case, this PR should not be merged. |
I'm using this patch to allow the user to remove a field by setting it to
None
.Basically, when an object attribute is set to
None
, rather than getting a validation error, the value is changed intomissing
and the field is removed.It seems to make sense to me and it works on my use case but it's hardly tested, so there may be corner cases I didn't think of.
Is there already a clean way to do that I didn't know about? If not, is it a feature you'd find useful?