-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Non-required fields with 'allow_null=True' should not imply a default value #5639
Conversation
Hi @RomuloOliveira, thanks for raising this. The docs do say that
To address a few things...
To clarify, the PR description is not completely correct, and this was not the final behavior.
Yes, this is related to
I don't think it's necessary for this PR. I'll address it separately. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Since encode#5639 we are unable to render fields that were rendered previously with `allow_null=True`. Backward compatibility is not preserved, because `required=True` and `read_only=True` are conflicting, so in our case, some fields just disappeared and it seems there is not way to get them back. I'm not sure about the fix, but the regression test is probably right.
In hindsight, while consistent with the @RomuloOliveira, was there a specific reason why these fields needed to be omitted from the serialized output? It's not clear to me what behavior this is enabling. |
@rpkilby I've made this to made In my opinion, it keeps things explicit and do not depend on any other flags combinations to work as expected. In this way, an user do not need to know all caveats and special behaviour that occurs when flags are combined. I noticed this because after upgrading to 3.7.2 some fields were not consistent with others in an API that I was developing and it took a lot of time to figure out what was causing it. I've thought at first it was a bug before seeing the discussion #5518. I thought it's a bit weird that This discussion was closed and so was #5518, but as an DRF user, I'm asked myself why use implicit values (which can interfere with another flags) and, why use it only in |
Since encode#5639 we are unable to render fields that were rendered previously with `allow_null=True`. Backward compatibility is not preserved, because `required=True` and `read_only=True` are conflicting, so in our case, some fields just disappeared and it seems there is not way to get them back. I'm not sure about the fix, but the regression test is probably right.
… default value (encode#5639)" This reverts commit 905a557. Closes encode#5708
… default value (encode#5639)" This reverts commit 905a557. Closes encode#5708
* Revert "Non-required fields with 'allow_null=True' should not imply a default value (#5639)" This reverts commit 905a557. Closes #5708 * Add test for allow_null + required=False Ref #5708: allow_null should imply default=None, even for non-required fields. * Re-order allow_null and default in field docs default is prior to allow_null. allow_null implies an outgoing default=None. * Adjust allow_null note.
* Revert "Non-required fields with 'allow_null=True' should not imply a default value (encode#5639)" This reverts commit 905a557. Closes encode#5708 * Add test for allow_null + required=False Ref encode#5708: allow_null should imply default=None, even for non-required fields. * Re-order allow_null and default in field docs default is prior to allow_null. allow_null implies an outgoing default=None. * Adjust allow_null note.
Prior to the #5518, all fields with
required=False
and without a explicit default value were not included in the output.After the patch, even non-required fields now had a default value, which is counter-intuitive if you look at the
required
documentation, which says Set to false if this field is not required to be present during deserialization.This PR restore the original
required=False
behaviour. Now, anyrequired=False
without a explicit default value should not be returned in the output.However, it keeps the implicit
default=None
for required fields introduced in #5518.Question I: Did I wrote the test in the correct class (
TestNotRequiredOutput
) or should it be in theTestDefaultOutput
class?Question II: Is there a reason that the implicit default
None
value forallow_null
is not in theField.get_default
function? Looking at theexcept
handling code, at first it did not sound right.Question III: Should I add to the documentation that
allow_null
implicitly set adefault=None
for required fields?That issue is already closed and it is not my intention to reopen it, but it's a special case that I did not fully understand why not forcing a explicit default value. E.g, what about
allow_blank
? Andmany=True
? Shouldn't they have a implicit default value too, using the same logic?