-
Notifications
You must be signed in to change notification settings - Fork 301
Fix/auto prefetch with m2m #333
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
Fix/auto prefetch with m2m #333
Conversation
That works through many to many field descriptors, previous way only worked through many to ones.
Updates the tests to use the optimised queryset views provided in views.py. For these to work, the serializer fields need to match the model names so I have just changed the related_name in the test models. This could be improved elsewhere, with the queryset prefetching based on the serializer. But for the most basic use case, this is fine.
Based on the relation descriptor.
Codecov Report
@@ Coverage Diff @@
## develop #333 +/- ##
==========================================
+ Coverage 91.68% 92.5% +0.82%
==========================================
Files 50 50
Lines 2368 2375 +7
==========================================
+ Hits 2171 2197 +26
+ Misses 197 178 -19
Continue to review full report at Codecov.
|
@@ -32,7 +36,7 @@ | |||
|
|||
class ModelViewSet(viewsets.ModelViewSet): | |||
def get_queryset(self, *args, **kwargs): | |||
qs = super().get_queryset(*args, **kwargs) | |||
qs = super(ModelViewSet, self).get_queryset(*args, **kwargs) |
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.
Oh, nice! Thanks for this one.
@bor3ham thanks for this work. I really like the improved coverage. 👍 |
Previously including relations with the optimised viewset through m2m's would crash as it was not correctly getting the model from the related descriptor
For example from the tests:
The tests already covered the right use case, but were not using the prefetching viewset.
I had to update the test models / serializers slightly to support the auto prefetching (which requires the field names in the serializer match the models). I will create an issue to address this as an improvement