-
Notifications
You must be signed in to change notification settings - Fork 299
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
Do select_related
for ?include
#591
Comments
We can have a separate Mixin for that or just add the logic to exiting one. class SelectForIncludesMixin(object):
def get_queryset(self):
"""
This viewset provides a helper attribute to select related models
based on the include specified in the URL.
__all__ can be used to specify a select which should be done regardless of the include
.. code:: python
# When MyViewSet is called with ?include=author it will prefetch author and authorbio
class MyViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
select_for_includes = {
'__all__': [],
'author': ['author', 'author__authorbio'],
'category.section': ['category']
}
"""
qs = super(SelectForIncludesMixin, self).get_queryset()
if not hasattr(self, 'select_for_includes'):
return qs
includes = self.request.GET.get('include', '').split(',')
for inc in includes + ['__all__']:
selects = self.select_for_includes.get(inc)
if selects:
qs = qs.select_related(*selects)
return qs |
@sliverc What do you think about it ? |
In corner cases it might be helpful to have a SelectForIncludesMixin but a user should not get into the habit of using I think having one mixin for select/prefetch for includes and one for auto prefetching would be a good idea. I guess we need to think about renaming the mixins (deprecating old name) as it might be confusing otherwise. Suggestions are welcome. Related to #337 |
I've found that we have nice
prefetch_for_includes
option on the ModelViewSet.I would also like
select_for_includes
mixin that will call addselect_related
and pass there values accordingly to the mapping.If you like the idea I'll create PR for that
The text was updated successfully, but these errors were encountered: