-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Incorrect Serializer behaviour for BooleanField(required=False) when passed a QueryDict #8300
Comments
Oh we have a same problem with class TestSerializer(serializers.Serializer):
test = serializers.NullBooleanField(required=False)
bla = TestSerializer(data=QueryDict())
bla.is_valid()
bla.validated_data
Quick fix for us atm |
You can also use serializer_partial = TestSerializer(data=data_querydict, partial=True)
serializer_partial.is_valid()
serializer_partial.validated_data And the BooleanField's test cases show the reason: for HTML checkboxes. django-rest-framework/tests/test_fields.py Lines 362 to 385 in f46c33e
Doc: https://www.django-rest-framework.org/api-guide/serializers/#partial-updates |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I have this exact same issue, but with a URLField. In my scenario, the field is not present on the data, but given it has |
I think a few more checks should be done here: https://github.com/encode/django-rest-framework/blob/master/rest_framework/fields.py#L440-L447. I'll try to put a PR up to fix this. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This is not stale, this bug is still present on DRF. |
bool_field_2 = BooleanField(required=False, default=False) Expected output should be:OrderedDict([('bool_field_2', False)]) send partial=True, It will not solve the problem completely, but will create new problems |
bool_field_2 = BooleanField(required=False, default=False) Expected output should be:OrderedDict([('bool_field_2', False)]) send partial=True, It will not solve the problem completely, but will create new problems |
Both types of empty dictionary input give the same result
SET the default value None situation
|
I would love your inputs and trying this PR #8614. also please contribute some suggested test cases to the test suite of this project. |
I want to contribute to this issue please give me permission |
I don’t think you need permission, you can report if the changes in #8614 fix the problems and if not submit a pull request! |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue is not stale, and still needs resolved from rest_framework.serializers import Serializer class TestAPI(APIView):
request.data is <QueryDict: {'id: ['uuid_value'], 'required_field1': ['true']}>
|
In the web api, I want to add the I think it's wrong to close the problem just because of the activity. |
you can use partial option to fix the problem |
Checklist
The Documentation states the following about the
required
keyword argument:"Normally an error will be raised if a field is not supplied during deserialization. Set to false if this field is not required to be present during deserialization.
Setting this to False also allows the object attribute or dictionary key to be omitted from output when serializing the instance. If the key is not present it will simply not be included in the output representation. "
However this doesn't work correctly with django.http.QueryDict. When passing a QueryDict to the Serializer, the not required fields are not omitted in the output but instead are set to False.
Affected Version:
django_rest_framework <= 3.12
found when upgrading from 3.11 to latest version
A Code Example
When executing this Example you will see that:
serializer1.validated_data == OrderedDict([('required', True)])
whileserializer2.validated_data == OrderedDict([('required', True), ('param', False)])
.This diverging behaviour is not present in other types of Field (Tested myself: CharField, IntegerField)
This is potentially an API breaking change
Possible Workaround
As Described here
Expected Behavior
not required fields are omitted in the output for both Dict and QueryDict
The text was updated successfully, but these errors were encountered: