-
Notifications
You must be signed in to change notification settings - Fork 31
5766 Add nested field filtering and ensure defer omitted fields #41
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
base: master
Are you sure you want to change the base?
Conversation
- Also added support for deferring filtered fields
hi @albertisfu Thanks for your work on this so far. If I could offer a few thoughts on how I see this?
|
for filtered_field in filter_fields: | ||
if "__" in filtered_field: | ||
parent, child = filtered_field.split("__", 1) | ||
self._nested_allow.setdefault(parent, []).append(child) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this have been written as a defaultdict?
def to_representation(self, instance):
"""This method prunes filtered fields from a nested serializer."""
representation = super(DynamicFieldsMixin, self).to_representation(instance)
# Apply nested omit on dicts and lists of dicts
for parent, omit_list in getattr(self, "_nested_omit", {}).items():
if parent not in representation:
continue
parent_instance = representation[parent]
def do_omit(d):
"""Helper to drop fields on a single dict"""
for child in omit_list:
d.pop(child, None)
if isinstance(parent_instance, dict):
do_omit(parent_instance)
elif isinstance(parent_instance, list):
for item in parent_instance:
if isinstance(item, dict):
do_omit(item) I was drawn to this code. Mostly because it introduces a large amount of new logic, but also because of the definition of a function inside of a for-loop. Upon reading it again, it seems to me that the way you are omitting/filtering is by rendering the entire serializer, including the expensive fields, then simply dropping them from the resultant dictionary. The problem is the work has already been done, and now you are doing extra work to drop it. So while the client of this request won't see the omitted fields and will see the filtered fields, they don't get the benefits promised them, because they already paid for the serialization of the missing fields. The way the original code tackled this was to manipulate the Is there a way to re-use this existing pattern in your solution? Without using that pattern, I don't think your solution sits within the spirit of what this library is about. |
Thank you, @jtrain, for your feedback! This PR is still a work in progress, but you're absolutely right that overriding |
No description provided.