@@ -35,15 +35,6 @@ class GenericAPIView(views.APIView):
35
35
slug_url_kwarg = 'slug' # Not provided in Django 1.3
36
36
slug_field = 'slug'
37
37
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
-
47
38
def get_serializer_context (self ):
48
39
"""
49
40
Extra context provided to the serializer class.
@@ -54,24 +45,6 @@ def get_serializer_context(self):
54
45
'view' : self
55
46
}
56
47
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
-
75
48
def get_serializer (self , instance = None , data = None ,
76
49
files = None , many = False , partial = False ):
77
50
"""
@@ -83,22 +56,6 @@ def get_serializer(self, instance=None, data=None,
83
56
return serializer_class (instance , data = data , files = files ,
84
57
many = many , partial = partial , context = context )
85
58
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
-
102
59
def get_pagination_serializer (self , page = None ):
103
60
"""
104
61
Return a serializer instance to use with paginated data.
@@ -111,16 +68,22 @@ class Meta:
111
68
context = self .get_serializer_context ()
112
69
return pagination_serializer_class (instance = page , context = context )
113
70
114
- def get_paginate_by (self , queryset ):
71
+ def get_paginate_by (self , queryset = None ):
115
72
"""
116
73
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`.
117
79
"""
118
80
if self .paginate_by_param :
119
81
query_params = self .request .QUERY_PARAMS
120
82
try :
121
83
return int (query_params [self .paginate_by_param ])
122
84
except (KeyError , ValueError ):
123
85
pass
86
+
124
87
return self .paginate_by
125
88
126
89
def paginate_queryset (self , queryset , page_size , paginator_class = Paginator ):
@@ -146,16 +109,54 @@ def paginate_queryset(self, queryset, page_size, paginator_class=Paginator):
146
109
'message' : str (e )
147
110
})
148
111
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
+
149
144
def get_queryset (self ):
150
145
"""
151
146
Get the list of items for this view.
152
-
153
147
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)
154
154
"""
155
155
if self .queryset is not None :
156
156
return self .queryset ._clone ()
157
157
158
158
if self .model is not None :
159
+ # TODO: Deprecation warning
159
160
return self .model ._default_manager .all ()
160
161
161
162
raise ImproperlyConfigured ("'%s' must define 'queryset' or 'model'"
@@ -164,10 +165,14 @@ def get_queryset(self):
164
165
def get_object (self , queryset = None ):
165
166
"""
166
167
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.
167
172
"""
168
173
# Determine the base queryset to use.
169
174
if queryset is None :
170
- queryset = self .get_queryset ()
175
+ queryset = self .filter_queryset ( self . get_queryset () )
171
176
172
177
# Perform the lookup filtering.
173
178
pk = self .kwargs .get (self .pk_url_kwarg , None )
@@ -177,8 +182,10 @@ def get_object(self, queryset=None):
177
182
if lookup is not None :
178
183
queryset = queryset .filter (** {self .lookup_kwarg : lookup })
179
184
elif pk is not None :
185
+ # TODO: Deprecation warning
180
186
queryset = queryset .filter (pk = pk )
181
187
elif slug is not None :
188
+ # TODO: Deprecation warning
182
189
queryset = queryset .filter (** {self .slug_field : slug })
183
190
else :
184
191
raise AttributeError ("Generic detail view %s must be called with "
@@ -197,6 +204,23 @@ def get_object(self, queryset=None):
197
204
198
205
return obj
199
206
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
+
200
224
201
225
### Concrete view classes that provide method handlers ###
202
226
### by composing the mixin classes with the base view. ###
0 commit comments