Skip to content

Meta fields are not filtered out dynamically #1052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 of 2 tasks
SafaAlfulaij opened this issue Feb 11, 2022 · 2 comments
Closed
1 of 2 tasks

Meta fields are not filtered out dynamically #1052

SafaAlfulaij opened this issue Feb 11, 2022 · 2 comments
Labels

Comments

@SafaAlfulaij
Copy link
Contributor

SafaAlfulaij commented Feb 11, 2022

Description of the Bug Report

DRF supports dynamically changing the fields returned per request. This works well (despite the performance issues, etc)

Meta fields though does not get filtered based on this properly. If the field is excluded, it will be rendered with a null value (since the actual value is not provided by DRF).

My expectation is that it does not render at all.

Culprits:

def get_field_names(self, declared_fields, info):
"""
We override the parent to omit explicity defined meta fields (such
as SerializerMethodFields) from the list of declared fields
"""
meta_fields = getattr(self.Meta, "meta_fields", [])
declared = OrderedDict()
for field_name in set(declared_fields.keys()):
field = declared_fields[field_name]
if field_name not in meta_fields:
declared[field_name] = field
fields = super().get_field_names(declared, info)
return list(fields) + list(getattr(self.Meta, "meta_fields", list()))

@classmethod
def extract_meta(cls, serializer, resource):
"""
Gathers the data from serializer fields specified in meta_fields and adds it to
the meta object.
"""
if hasattr(serializer, "child"):
meta = getattr(serializer.child, "Meta", None)
else:
meta = getattr(serializer, "Meta", None)
meta_fields = getattr(meta, "meta_fields", [])
data = OrderedDict()
for field_name in meta_fields:
data.update({field_name: resource.get(field_name)})
return data

Checklist

  • Certain that this is a bug (if unsure or you have a question use discussions instead)
  • Code snippet or unit test added to reproduce bug
@SafaAlfulaij SafaAlfulaij changed the title Meta fields are not filtered out dynamiclly Meta fields are not filtered out dynamically Feb 11, 2022
@sliverc
Copy link
Member

sliverc commented Feb 12, 2022

As I see it is this basically the same bug as described in #332 as the sparse fields mixin uses this feature to dynamically change the fields.

@SafaAlfulaij
Copy link
Contributor Author

Indeed. Closing this then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants