-
-
Notifications
You must be signed in to change notification settings - Fork 7k
not required read_only Fields with allow_null #5708
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
Conversation
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.
@@ -384,10 +384,16 @@ def create(self, validated_data): | |||
def test_not_required_output_for_allow_null_field(self): | |||
class ExampleSerializer(serializers.Serializer): | |||
omitted = serializers.CharField(required=False, allow_null=True) | |||
ommited_read_only = serializers.CharField( | |||
required=False, | |||
read_only=True, |
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.
btw, the required=False
is unnecessary here, as it's implied by read_only=True
.
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.
I can take it out if you prefer
In hindsight, I think the behavior in #5639 is not correct. Fields with |
To reiterate, I'd argue that it's not correct for Barring some reason for the distinction, I'm inclined to partially revert #5639. |
@ticosax What was the behaviour before the 3.7.2 version? I've opened the #5639 because the #5518 brought backwards incompatible changes that broke code in an application of mine. I'm not sure if the compatibility was broken at #5639 or it's a confusion with implicit values, but if it was broken at it, I think it is ok to reversed it. I've provided more details of the why here. |
@RomuloOliveira After #5639 we are unable include fields with |
@ticosax Are these fields automatically generated by a |
declared.
eventually. it doesn't change anything though.
Sorry, I don't see how I could do better than the regression test this PR is adding. IMO there is everything you ask. |
@rpkilby We've had a sequence of little changes here. Each time it's not been exactly clear what the supported use-cases and behaviours are. I think we need to review the history, write it up in black and white (for us and the docs) and make at most one more change to resolve this. Do you have capacity to go over the history here and draw up an overview? If so that would be awesome. If not I'm happy to take that on. |
@carltongibson Yep, I'd be happy to take that on. |
Some findings in this regard:
class Serializer(DefaultBaseHyperlinkedModelSerializer):
phone_numbers = serializers.JSONField(
source='profile.phone_numbers',
read_only=True,
# Because of https://github.com/encode/django-rest-framework/pull/5518
allow_null=True,
# XXX: causes field to show up in _writable_fields.
default=None,
) e37619f added tests for this, but changed something else. It basically causes The error then is:
Not having read_only fields in
|
Thanks for the writeup @blueyed. |
I think this can be skipped/closed in favor of #5727 - at least that's what is working for us now. |
Ref encode#5708: allow_null should imply default=None, even for non-required fields.
… default value (encode#5639)" This reverts commit 905a557. Closes encode#5708
Ref encode#5708: allow_null should imply default=None, even for non-required fields. flake8
… 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.
Description
Since #5639 we are unable to render fields that were rendered previously with
allow_null=True
.Backward compatibility is not preserved, because
required=True
andread_only=True
are conflicting, so in our case, some fields just disappeared and it seems there is no way to get them back.I'm not sure about the fix, but the regression test is probably right.