-
-
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
Updated NestedBoundField as_form_field to handle None #3464
Conversation
This fixes the html rendering of a nested bound field that is allowed to be null. For example, if my API has related model serialisers, sort of like this: class PersonSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Person fields = ('id', 'url', 'first_name', 'last_name') class GunSerializer(serializers.HyperlinkedModelSerializer): pointing_at = PersonSerializer(many=False, allow_null=True, required=False) class Meta: model = Gun fields = ('id', 'url', 'brand', 'pointing_at') The 'pointing_at' field becomes a NestedBoundField. The API works fine with the null reference in the 'json-only' form (i.e. ?format=json). However, when the HTML form tries to render, I get an error 'NoneType' object has no attribute 'items'. This change prevents the FOR loop from attempting to access .items() on the self.value when it is None. NB: This does not fix the issue with rendering the put-object-form correctly. Perhaps there should be a mechanism to allow the PrimaryKeyRelatedField HTML picker?
Nice. This fix makes browsable api not crash in a similar situation I came across. In my case, the exception was |
# Conflicts: # rest_framework/utils/serializer_helpers.py
Yes, I can't see the harm in it being merged. |
Well, I guess a test case would help to get this reviewed. |
@xordoquy is there an existing set of tests for the |
Hmm, I've dug into the history a bit, and it seems that there have been a few attempts at fixing this. In particular, this is related to the changes for these issues:
As far as I can tell, the current implementation ensures in the In some ways I think my changes might have got a bit lucky, by using the 'truthy/falsy' behaviour of empty strings etc. |
Well, since the check is already done in the init(), it should probably remain there. Just make sure it catches other cases too (e.g. empty strings), not only None. Right?
|
Closing this for now - welcome to reassess if required. |
In my case, it is about an "inner" nested field with |
This fixes the html rendering of a nested bound field that is allowed to be null.
For example, if my API has related model serialisers, sort of like this:
The 'pointing_at' field becomes a
NestedBoundField
. The API works fine with the null reference in the 'json-only' form (i.e. ?format=json). However, when the HTML form tries to render, I get an error'NoneType' object has no attribute 'items'
.This change prevents the FOR loop from attempting to access .items() on the self.value when it is None.
NB: This does not fix the issue with rendering the put-object-form correctly. Perhaps there should be a mechanism to allow the PrimaryKeyRelatedField HTML picker?