8000 Cleaning up around bits of API that will be pending deprecation · dhepper/django-rest-framework@9bb1277 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9bb1277

Browse files
committed
Cleaning up around bits of API that will be pending deprecation
1 parent 1de6cff commit 9bb1277

File tree

3 files changed

+75
-56
lines changed

3 files changed

+75
-56
lines changed

rest_framework/generics.py

Lines changed: 70 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,6 @@ class GenericAPIView(views.APIView):
3535
slug_url_kwarg = 'slug' # Not provided in Django 1.3
3636
slug_field = 'slug'
3737

38-
def filter_queryset(self, queryset):
39-
"""
40-
Given a queryset, filter it with whichever filter backend is in use.
41-
"""
42-
if not self.filter_backend:
43-
return queryset
44-
backend = self.filter_backend()
45-
return backend.filter_queryset(self.request, queryset, self)
46-
4738
def get_serializer_context(self):
4839
"""
4940
Extra context provided to the serializer class.
@@ -54,24 +45,6 @@ def get_serializer_context(self):
5445
'view': self
5546
}
5647

57-
def get_serializer_class(self):
58-
"""
59-
Return the class to use for the serializer.
60-
61-
Defaults to using `self.serializer_class`, falls back to constructing a
62-
model serializer class using `self.model_serializer_class`, with
63-
`self.model` as the model.
64-
"""
65-
serializer_class = self.serializer_class
66-
67-
if serializer_class is None:
68-
class DefaultSerializer(self.model_serializer_class):
69-
class Meta:
70-
model = self.model
71-
serializer_class = DefaultSerializer
72-
73-
return serializer_class
74-
7548
def get_serializer(self, instance=None, data=None,
7649
files=None, many=False, partial=False):
7750
"""
@@ -83,22 +56,6 @@ def get_serializer(self, instance=None, data=None,
8356
return serializer_class(instance, data=data, files=files,
8457
many=many, partial=partial, context=context)
8558

86-
def pre_save(self, obj):
87-
"""
88-
Placeholder method for calling before saving an object.
89-
May be used eg. to set attributes on the object that are implicit
90-
in either the request, or the url.
91-
"""
92-
pass
93-
94-
def post_save(self, obj, created=False):
95-
"""
96-
Placeholder method for calling after saving an object.
97-
"""
98-
pass
99-
100-
# Pagination
101-
10259
def get_pagination_serializer(self, page=None):
10360
"""
10461
Return a serializer instance to use with paginated data.
@@ -111,16 +68,22 @@ class Meta:
11168
context = self.get_serializer_context()
11269
return pagination_serializer_class(instance=page, context=context)
11370

114-
def get_paginate_by(self, queryset):
71+
def get_paginate_by(self, queryset=None):
11572
"""
11673
Return the size of pages to use with pagination.
74+
75+
If `PAGINATE_BY_PARAM` is set it will attempt to get the page size
76+
from a named query parameter in the url, eg. ?page_size=100
77+
78+
Otherwise defaults to using `self.paginate_by`.
11779
"""
11880
if self.paginate_by_param:
11981
query_params = self.request.QUERY_PARAMS
12082
try:
12183
return int(query_params[self.paginate_by_param])
12284
except (KeyError, ValueError):
12385
pass
86+
12487
return self.paginate_by
12588

12689
def paginate_queryset(self, queryset, page_size, paginator_class=Paginator):
@@ -146,16 +109,54 @@ def paginate_queryset(self, queryset, page_size, paginator_class=Paginator):
146109
'message': str(e)
147110
})
148111

112+
def filter_queryset(self, queryset):
113+
"""
114+
Given a queryset, filter it with whichever filter backend is in use.
115+
"""
116+
if not self.filter_backend:
117+
return queryset
118+
backend = self.filter_backend()
119+
return backend.filter_queryset(self.request, queryset, self)
120+
121+
### The following methods provide default implementations
122+
### that you may want to override for more complex cases.
123+
124+
def get_serializer_class(self):
125+
"""
126+
Return the class to use for the serializer.
127+
Defaults to using `self.serializer_class`.
128+
129+
You may want to override this if you need to provide different
130+
serializations depending on the incoming request.
131+
132+
(Eg. admins get full serialization, others get basic serilization)
133+
"""
134+
serializer_class = self.serializer_class
135+
if serializer_class is not None:
136+
return serializer_class
137+
138+
# TODO: Deprecation warning
139+
class DefaultSerializer(self.model_serializer_class):
140+
class Meta:
141+
model = self.model
142+
return DefaultSerializer
143+
149144
def get_queryset(self):
150145
"""
151146
Get the list of items for this view.
152-
153147
This must be an iterable, and may be a queryset.
148+
Defaults to using `self.queryset`.
149+
150+
You may want to override this if you need to provide different
151+
querysets depending on the incoming request.
152+
153+
(Eg. return a list of items that is specific to the user)
154154
"""
155155
if self.queryset is not None:
156156
return self.queryset._clone()
157157

158158
if self.model is not None:
159+
# TODO: Deprecation warning
159160
return self.model._default_manager.all()
160161

161162
raise ImproperlyConfigured("'%s' must define 'queryset' or 'model'"
@@ -164,10 +165,14 @@ def get_queryset(self):
164165
def get_object(self, queryset=None):
165166
"""
166167
Returns the object the view is displaying.
168+
169+
You may want to override this if you need to provide non-standard
170+
queryset lookups. Eg if objects are referenced using multiple
171+
keyword arguments in the url conf.
167172
"""
168173
# Determine the base queryset to use.
169174
if queryset is None:
170-
queryset = self.get_queryset()
175+
queryset = self.filter_queryset(self.get_queryset())
171176

172177
# Perform the lookup filtering.
173178
pk = self.kwargs.get(self.pk_url_kwarg, None)
@@ -177,8 +182,10 @@ def get_object(self, queryset=None):
177182
if lookup is not None:
178183
queryset = queryset.filter(**{self.lookup_kwarg: lookup})
179184
elif pk is not None:
185+
# TODO: Deprecation warning
180186
queryset = queryset.filter(pk=pk)
181187
elif slug is not None:
188+
# TODO: Deprecation warning
182189
queryset = queryset.filter(**{self.slug_field: slug})
183190
else:
184191
raise AttributeError("Generic detail view %s must be called with "
@@ -197,6 +204,23 @@ def get_object(self, queryset=None):
197204

198205
return obj
199206

207+
### The following methods are intended to be overridden.
208+
209+
def pre_save(self, obj):
210+
"""
211+
Placeholder method for calling before saving an object.
212+
213+
May be used to set attributes on the object that are implicit
214+
in either the request, or the url.
215+
"""
216+
pass
217+
218+
def post_save(self, obj, created=False):
219+
"""
220+
Placeholder method for calling after saving an object.
221+
"""
222+
pass
223+
200224

201225
### Concrete view classes that provide method handlers ###
202226
### by composing the mixin classes with the base view. ###

rest_framework/mixins.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ class ListModelMixin(object):
6767
empty_error = "Empty list and '%(class_name)s.allow_empty' is False."
6868

6969
def list(self, request, *args, **kwargs):
70-
queryset = self.get_queryset()
71-
self.object_list = self.filter_queryset(queryset)
70+
self.object_list = self.filter_queryset(self.get_queryset())
7271

7372
# Default is to allow empty querysets. This can be altered by setting
7473
# `.allow_empty = False`, to raise 404 errors on empty querysets.
@@ -79,7 +78,7 @@ def list(self, request, *args, **kwargs):
7978

8079
# Pagination size is set by the `.paginate_by` attribute,
8180
# which may be `None` to disable pagination.
82-
page_size = self.get_paginate_by(self.object_list)
81+
page_size = self.get_paginate_by()
8382
if page_size:
8483
packed = self.paginate_queryset(self.object_list, page_size)
8584
paginator, page, queryset, is_paginated = packed
@@ -96,9 +95,7 @@ class RetrieveModelMixin(object):
9695
Should be mixed in with `SingleObjectAPIView`.
9796
"""
9897
def retrieve(self, request, *args, **kwargs):
99-
queryset = self.get_queryset()
100-
filtered_queryset = self.filter_queryset(queryset)
101-
self.object = self.get_object(filtered_queryset)
98+
self.object = self.get_object()
10299
serializer = self.get_serializer(self.object)
103100
return Response(serializer.data)
104101

rest_framework/viewsets.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,12 @@ class ModelViewSet(mixins.CreateModelMixin,
7676
mixins.DestroyModelMixin,
7777
mixins.ListModelMixin,
7878
ViewSetMixin,
79-
generics.MultipleObjectAPIView,
80-
generics.SingleObjectAPIView):
79+
generics.GenericAPIView):
8180
pass
8281

8382

8483
class ReadOnlyModelViewSet(mixins.RetrieveModelMixin,
8584
mixins.ListModelMixin,
8685
ViewSetMixin,
87-
generics.MultipleObjectAPIView,
88-
generics.SingleObjectAPIView):
86+
generics.GenericAPIView):
8987
pass

0 commit comments

Comments
 (0)
0