@@ -35,7 +35,7 @@ class PrefetchForIncludesHelperMixin(object):
35
35
36
36
def __init__ (self , * args , ** kwargs ):
37
37
warnings .warn ("PrefetchForIncludesHelperMixin is deprecated. "
38
- "Use AutoPreloadMixin instead" ,
38
+ "Use PreloadIncludesMixin instead" ,
39
39
DeprecationWarning )
40
40
super (PrefetchForIncludesHelperMixin , self ).__init__ (* args , ** kwargs )
41
41
@@ -70,7 +70,7 @@ class MyViewSet(viewsets.ModelViewSet):
70
70
return qs
71
71
72
72
73
- class AutoPreloadMixin (object ):
73
+ class PreloadIncludesMixin (object ):
74
74
"""
75
75
This mixin provides a helper attributes to select or prefetch related models
76
76
based on the include specified in the URL.
@@ -99,22 +99,30 @@ def get_prefetch_related(self, include):
99
99
return getattr (self , 'prefetch_for_includes' , {}).get (include , None )
100
100
101
101
def get_queryset (self , * args , ** kwargs ):
102
- """ This mixin adds automatic prefetching for OneToOne and ManyToMany fields. """
103
- qs = super (AutoPreloadMixin , self ).get_queryset (* args , ** kwargs )
104
- included_resources = get_included_resources (self .request )
102
+ qs = super (PreloadIncludesMixin , self ).get_queryset (* args , ** kwargs )
105
103
104
+ included_resources = get_included_resources (self .request )
106
105
for included in included_resources + ['__all__' ]:
107
- # Custom defined "select_related" and "prefetch_related" is a priority
106
+
108
107
select_related = self .get_select_related (included )
109
108
if select_related is not None :
110
109
qs = qs .select_related (* select_related )
111
- continue
112
110
113
111
prefetch_related = self .get_prefetch_related (included )
114
112
if prefetch_related is not None :
115
113
qs = qs .prefetch_related (* prefetch_related )
116
- continue
117
114
115
+ return qs
116
+
117
+
118
+ class AutoPreloadMixin (object ):
119
+
120
+ def get_queryset (self , * args , ** kwargs ):
121
+ """ This mixin adds automatic prefetching for OneToOne and ManyToMany fields. """
122
+ qs = super (AutoPreloadMixin , self ).get_queryset (* args , ** kwargs )
123
+ included_resources = get_included_resources (self .request )
124
+
125
+ for included in included_resources + ['__all__' ]:
118
126
# If include was not defined, trying to resolve it automatically
119
127
included_model = None
120
128
levels = included .split ('.' )
@@ -252,6 +260,7 @@ def get_related_instance(self):
252
260
253
261
254
262
class ModelViewSet (AutoPreloadMixin ,
263
+ PreloadIncludesMixin ,
255
264
RelatedMixin ,
256
265
viewsets .ModelViewSet ):
257
266
pass
0 commit comments