Skip to content

Commit d756fd7

Browse files
committed
Remove AutoPrefetchMixin
1 parent ca627a9 commit d756fd7

File tree

3 files changed

+6
-52
lines changed

3 files changed

+6
-52
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ any parts of the framework not mentioned in the documentation should generally b
1313
### Changed
1414

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

1718
### Deprecated
1819

example/tests/unit/test_filter_schema_params.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class DummyEntryViewSet(EntryViewSet):
2020
}
2121

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

2725

rest_framework_json_api/views.py

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.db.models.manager import Manager
1212
from django.db.models.query import QuerySet
1313
from django.urls import NoReverseMatch
14+
from django.utils.module_loading import import_string as import_class_from_dotted_path
1415
from rest_framework import generics, viewsets
1516
from rest_framework.exceptions import MethodNotAllowed, NotFound
1617
from rest_framework.fields import get_attribute
@@ -30,54 +31,6 @@
3031
)
3132

3233

33-
class PreloadIncludesMixin(object):
34-
"""
35-
This mixin provides a helper attributes to select or prefetch related models
36-
based on the include specified in the URL.
37-
38-
__all__ can be used to specify a prefetch which should be done regardless of the include
39-
40-
41-
.. code:: python
42-
43-
# When MyViewSet is called with ?include=author it will prefetch author and authorbio
44-
class MyViewSet(viewsets.ModelViewSet):
45-
queryset = Book.objects.all()
46-
prefetch_for_includes = {
47-
'__all__': [],
48-
'category.section': ['category']
49-
}
50-
select_for_includes = {
51-
'__all__': [],
52-
'author': ['author', 'author__authorbio'],
53-
}
54-
"""
55-
56-
def get_select_related(self, include):
57-
return getattr(self, "select_for_includes", {}).get(include, None)
58-
59-
def get_prefetch_related(self, include):
60-
return getattr(self, "prefetch_for_includes", {}).get(include, None)
61-
62-
def get_queryset(self, *args, **kwargs):
63-
qs = super(PreloadIncludesMixin, self).get_queryset(*args, **kwargs)
64-
65-
included_resources = get_included_resources(
66-
self.request, self.get_serializer_class()
67-
)
68-
for included in included_resources + ["__all__"]:
69-
70-
select_related = self.get_select_related(included)
71-
if select_related is not None:
72-
qs = qs.select_related(*select_related)
73-
74-
prefetch_related = self.get_prefetch_related(included)
75-
if prefetch_related is not None:
76-
qs = qs.prefetch_related(*prefetch_related)
77-
78-
return qs
79-
80-
8134
class AutoPrefetchMixin(object):
8235
def get_queryset(self, *args, **kwargs):
8336
"""This mixin adds automatic prefetching for OneToOne and ManyToMany fields."""
@@ -182,6 +135,8 @@ def get_related_serializer_class(self):
182135
False
183136
), 'Either "included_serializers" or "related_serializers" should be configured'
184137

138+
if not isinstance(_class, type):
139+
return import_class_from_dotted_path(_class)
185140
return _class
186141

187142
return parent_serializer_class
@@ -215,13 +170,13 @@ def get_related_instance(self):
215170

216171

217172
class ModelViewSet(
218-
AutoPrefetchMixin, PreloadIncludesMixin, RelatedMixin, viewsets.ModelViewSet
173+
AutoPrefetchMixin, RelatedMixin, viewsets.ModelViewSet
219174
):
220175
http_method_names = ["get", "post", "patch", "delete", "head", "options"]
221176

222177

223178
class ReadOnlyModelViewSet(
224-
AutoPrefetchMixin, PreloadIncludesMixin, RelatedMixin, viewsets.ReadOnlyModelViewSet
179+
AutoPrefetchMixin, RelatedMixin, viewsets.ReadOnlyModelViewSet
225180
):
226181
http_method_names = ["get", "post", "patch", "delete", "head", "options"]
227182

0 commit comments

Comments
 (0)