Skip to content

Accessing serializer.fields inside a nested serializer breaks the use of only/exclude #52

@philipstarkey

Description

@philipstarkey

For some reason that I can't quite figure out, it seems that accessing self.fields inside a nested and expanded serializer causes the only/exclude URL query args to be ignored for that nested serializer (as in I get all fields in the response for any nested serializer that accesses self.fields inside __init__).

This seems odd to me, and I assume is caused by some side-effect of the field binding that rest_framework does when self.fields is accessed. I've, so far, managed to implement work arounds that either use extra_kwargs or self.get_fields() but many examples/stack-overflow answers suggest using self.fields (e.g. to override the widget used for a field in the automatically generated create/update form in the browsable API).

Accessing self.fields in __init__ of the top level serializer doesn't break only/exclude

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions