Skip to content

should required option be valid for output? #2237

@Catstyle

Description

@Catstyle

i had upgraded from 2.4.4 to 3.0, and fix some incompatable things, but still i encountered some troubles
assumed we have a Serializer like this:

class Foo(serializers.Serializer):

    items = serializer.ListField(child=XXX)
    bar = serializers.IntegerField(required=False)

in my views i called

resp = service.get_data(...)
# resp = {'items': [...], 'foo': 'xxx'}
serializer = self.get_serializer(resp)
return Response(serializer.data)

then, i got errors
1, TypeError: 'builtin_function_or_method' object is not iterable
after some tracing, i found that fields.py:get_attribute handling the dict after getattr, but unfortunately i got key name the same with dict.items method name, so i think we should check __getitem__ value first then the getattr value

fixed 81d0b74
2, AttributeError: 'dict' object has no attribute 'bar'
after i fixed above error, i ran into another error, it seems that required=False doesn't work
it started from serializer.data -> serializer.to_representation, and iterate all field.get_attribute(instance)
so should we pass field.required to get_attribute and return None if required is False?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions