-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Improve ListSerializer #9244
base: master
Are you sure you want to change the base?
Improve ListSerializer #9244
Conversation
if not self.instance and self.parent and self.parent.instance: | ||
rel_mgr = get_attribute(self.parent.instance, self.source_attrs) | ||
self.instance = rel_mgr.all() if rel_mgr else None |
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.
I'm not sure this is the best place to do this. I'd like to hear the opinion of someone more familiar with this codebase.
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.
can you give a more verbose name to rel_mgr please? also share a bit more of what this would be doing?
I just realized there's a side case here. The child may not be a ModelSerializer. I'll leave it as a draft while I think about it, suggestions are welcome. We could create a new ModelListSerializer class with these changes and have it as the default list_serializer_class of ModelSerializer. @auvipy let me know what you think. |
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.
I think we should have appropriate test cases for this before even trying to fix them
Agree, I'll do it |
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.
as you can see the CI builds are failing, can you please rebase and make it green at least? possibly with very basic test? you can try incremental approach to fix the problem
Yes, I will do it. Sorry for the delay, I'm struggling to find time to continue this PR, any help is welcome |
Description
#8979 prevents list serializers from having data and instances with different lengths:
https://github.com/encode/django-rest-framework/pull/8979/files#diff-c33f1f011c9f5cf3ed1357b809ebf2b4ed0b808b227c6c007291bf9b259422ecR612-R617
This design choice breaks some use cases. Let's say we want the list serializer to delete from queryset all elements that are not provided in the data during an update. The queryset will be bigger than the data in this case:
The intention of the original PR is good, it solves the problem of not passing initial data and instances to child serializers correctly. But instead of indexing, it's better to relate the elements by id, so we can have different lengths. This PR addresses the issue.
refs #8926 #8979