I have read a changelog for drf3 but its still unclear for me. Previously i have following serializer:
class TestSerializer(serializers.Serializer):
att1= serializers.CharField()
att2= serializers.CharField()
att3= serializers.CharField(required=False)
And when i was passing object with only att1 and att2 values in it - it was working fine, no errors, no attribute in the output. But now if i don't pas att3 i got error
Got KeyError when attempting to get a value for field
att3on serializerTestSerializer.
The serializer field might be named incorrectly and not match any attribute or key on theEasyDictinstance.
Original exception text was: att3'.
But according to release notes:
required=False: The value does not need to be present in the input
So the code seems valid for me or i don't understand something.
data = TestSerializer(s.get_results()).data
Where get_results instance of EasyDict with missing att3:
class EasyDict(dict):
def __init__(self, d=None, **kwargs):
if d is None:
d = {}
if kwargs:
d.update(**kwargs)
for k, v in d.items():
setattr(self, k, v)
# Class attributes
for k in self.__class__.__dict__.keys():
if not (k.startswith('__') and k.endswith('__')):
setattr(self, k, getattr(self, k))
def __setattr__(self, name, value):
if isinstance(value, (list, tuple)):
value = [self.__class__(x) if isinstance(x, dict) else x for x in value]
else:
value = self.__class__(value) if isinstance(value, dict) else value
super(EasyDict, self).__setattr__(name, value)
self[name] = value
It was working perfectly fine in drf2, but got this error after upgrading to drf3.