Skip to content

Instantly share code, notes, and snippets.

@geeky-sh
Created January 1, 2020 09:57
Show Gist options
  • Save geeky-sh/4598b6140abe37b4853b8bc1ac84ab6b to your computer and use it in GitHub Desktop.
Save geeky-sh/4598b6140abe37b4853b8bc1ac84ab6b to your computer and use it in GitHub Desktop.
Query Dict issue in serialisers
from rest_framework import serializers
class T(serializers.Serializer):
a = serializers.BooleanField()
b = serializers.BooleanField(required=False)
c = serializers.BooleanField(required=False)
d = serializers.CharField()
def __init__(self, data, **kwargs):
super().__init__(instance=None, data=data, **kwargs)
if data.get('a'):
self.fields['b'].required = True
else:
self.fields['c'].required = True
from django.http import QueryDict
d = QueryDict('', mutable=True)
d.update({'a': 1, 'b': 1, 'd': 1})
x = T(data=d)
x.is_valid()
x.validated_data
# expected - OrderedDict([('a', True), ('b', True), ('d', '1')])
# actual - OrderedDict([('a', True), ('b', True), ('c', False), ('d', '1')])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment