Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove AutoPrefetchMixin
  • Loading branch information
jarekwg committed Jul 22, 2021
commit d756fd7ad9d89ec41c551399b1733c885ee173f2
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ any parts of the framework not mentioned in the documentation should generally b
### Changed

* Moved resolving of `included_serialzers` and `related_serializers` classes to serializer's meta class.
* Removed `PreloadIncludesMixin`, as the logic did not work when nesting includes, and the laborious effort needed in its manual config was unnecessary.

### Deprecated

Expand Down
2 changes: 0 additions & 2 deletions example/tests/unit/test_filter_schema_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class DummyEntryViewSet(EntryViewSet):
}

def __init__(self, **kwargs):
# dummy up self.request since PreloadIncludesMixin expects it to be defined
self.request = None
super(DummyEntryViewSet, self).__init__(**kwargs)


Expand Down
55 changes: 5 additions & 50 deletions rest_framework_json_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.db.models.manager import Manager
from django.db.models.query import QuerySet
from django.urls import NoReverseMatch
from django.utils.module_loading import import_string as import_class_from_dotted_path
from rest_framework import generics, viewsets
from rest_framework.exceptions import MethodNotAllowed, NotFound
from rest_framework.fields import get_attribute
Expand All @@ -30,54 +31,6 @@
)


class PreloadIncludesMixin(object):
"""
This mixin provides a helper attributes to select or prefetch related models
based on the include specified in the URL.

__all__ can be used to specify a prefetch 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()
prefetch_for_includes = {
'__all__': [],
'category.section': ['category']
}
select_for_includes = {
'__all__': [],
'author': ['author', 'author__authorbio'],
}
"""

def get_select_related(self, include):
return getattr(self, "select_for_includes", {}).get(include, None)

def get_prefetch_related(self, include):
return getattr(self, "prefetch_for_includes", {}).get(include, None)

def get_queryset(self, *args, **kwargs):
qs = super(PreloadIncludesMixin, self).get_queryset(*args, **kwargs)

included_resources = get_included_resources(
self.request, self.get_serializer_class()
)
for included in included_resources + ["__all__"]:

select_related = self.get_select_related(included)
if select_related is not None:
qs = qs.select_related(*select_related)

prefetch_related = self.get_prefetch_related(included)
if prefetch_related is not None:
qs = qs.prefetch_related(*prefetch_related)

return qs


class AutoPrefetchMixin(object):
def get_queryset(self, *args, **kwargs):
"""This mixin adds automatic prefetching for OneToOne and ManyToMany fields."""
Expand Down Expand Up @@ -182,6 +135,8 @@ def get_related_serializer_class(self):
False
), 'Either "included_serializers" or "related_serializers" should be configured'

if not isinstance(_class, type):
return import_class_from_dotted_path(_class)
return _class

return parent_serializer_class
Expand Down Expand Up @@ -215,13 +170,13 @@ def get_related_instance(self):


class ModelViewSet(
AutoPrefetchMixin, PreloadIncludesMixin, RelatedMixin, viewsets.ModelViewSet
AutoPrefetchMixin, RelatedMixin, viewsets.ModelViewSet
):
http_method_names = ["get", "post", "patch", "delete", "head", "options"]


class ReadOnlyModelViewSet(
AutoPrefetchMixin, PreloadIncludesMixin, RelatedMixin, viewsets.ReadOnlyModelViewSet
AutoPrefetchMixin, RelatedMixin, viewsets.ReadOnlyModelViewSet
):
http_method_names = ["get", "post", "patch", "delete", "head", "options"]

Expand Down