Skip to content

Get rid of warning #27

@dbrgn

Description

@dbrgn

I have a use case where I need to dynamically determine the nested serializer like this:

    def get_config(self, tracker: Tracker):
        config = tracker.get_config()
        if config is not None:
            serializer = TRACKER_CONFIG_SERIALIZERS.get(tracker.model)
            if serializer is None:
                logger.warning('Could not find serializer for model %s', tracker.model)
                return None
            return serializer(config).data
        return None

This works but results in a warning, because the nested serializer does not have access to the request in the context.

If we pass in the context...

            return serializer(config, context=self.context).data

...then the warning is gone, but the filtering is applied to those fields too.

The problem is that drf-dynamic-fields cannot know that this serializer is a nested serializer because it's used like a root serializer. And I'm afraid of playing some tricks with parent/child attributes, since that can go wrong easily.

I see two approaches to solve this:

  • Remove the warning (no filtering if there's no request in the context)
  • Add a special field to the context (e.g. dynamic_fields_ignore) that is queried in order to ignore the filtering on this serializer.

I'm undecided. What do you think @jtrain?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions