Skip to content

Commit 2a43d9d

Browse files
committed
Merge pull request #2486 from maryokhin/master
Cleanup '.model' shortcut from code and docs
2 parents 60a63ff + e720927 commit 2a43d9d

File tree

5 files changed

+10
-19
lines changed

5 files changed

+10
-19
lines changed

docs/api-guide/generic-views.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,13 @@ The following attributes are used to control pagination when used with list view
9393

9494
* `filter_backends` - A list of filter backend classes that should be used for filtering the queryset. Defaults to the same value as the `DEFAULT_FILTER_BACKENDS` setting.
9595

96-
**Deprecated attributes**:
97-
98-
* `model` - This shortcut may be used instead of setting either (or both) of the `queryset`/`serializer_class` attributes. The explicit style is preferred over the `.model` shortcut, and usage of this attribute is now deprecated.
99-
10096
### Methods
10197

10298
**Base methods**:
10399

104100
#### `get_queryset(self)`
105101

106-
Returns the queryset that should be used for list views, and that should be used as the base for lookups in detail views. Defaults to returning the queryset specified by the `queryset` attribute, or the default queryset for the model if the `model` shortcut is being used.
102+
Returns the queryset that should be used for list views, and that should be used as the base for lookups in detail views. Defaults to returning the queryset specified by the `queryset` attribute.
107103

108104
This method should always be used rather than accessing `self.queryset` directly, as `self.queryset` gets evaluated only once, and those results are cached for all subsequent requests.
109105

@@ -153,7 +149,7 @@ For example:
153149

154150
#### `get_serializer_class(self)`
155151

156-
Returns the class that should be used for the serializer. Defaults to returning the `serializer_class` attribute, or dynamically generating a serializer class if the `model` shortcut is being used.
152+
Returns the class that should be used for the serializer. Defaults to returning the `serializer_class` attribute.
157153

158154
May be overridden to provide dynamic behavior, such as using different serializers for read and write operations, or providing different serializers to different types of users.
159155

docs/api-guide/routers.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ There are two mandatory arguments to the `register()` method:
2828

2929
Optionally, you may also specify an additional argument:
3030

31-
* `base_name` - The base to use for the URL names that are created. If unset the basename will be automatically generated based on the `model` or `queryset` attribute on the viewset, if it has one. Note that if the viewset does not include a `model` or `queryset` attribute then you must set `base_name` when registering the viewset.
31+
* `base_name` - The base to use for the URL names that are created. If unset the basename will be automatically generated based on the `queryset` attribute of the viewset, if it has one. Note that if the viewset does not include a `queryset` attribute then you must set `base_name` when registering the viewset.
3232

3333
The example above would generate the following URL patterns:
3434

docs/topics/3.0-announcement.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ This code *would be valid* in `2.4.3`:
665665
class Meta:
666666
model = Account
667667

668-
However this code *would not be valid* in `2.4.3`:
668+
However this code *would not be valid* in `3.0`:
669669

670670
# Missing `queryset`
671671
class AccountSerializer(serializers.Serializer):

rest_framework/routers.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,13 @@ def get_default_base_name(self, viewset):
130130
If `base_name` is not specified, attempt to automatically determine
131131
it from the viewset.
132132
"""
133-
# Note that `.model` attribute on views is deprecated, although we
134-
# enforce the deprecation on the view `get_serializer_class()` and
135-
# `get_queryset()` methods, rather than here.
136-
model_cls = getattr(viewset, 'model', None)
137133
queryset = getattr(viewset, 'queryset', None)
138-
if model_cls is None and queryset is not None:
139-
model_cls = queryset.model
140134

141-
assert model_cls, '`base_name` argument not specified, and could ' \
135+
assert queryset is not None, '`base_name` argument not specified, and could ' \
142136
'not automatically determine the name from the viewset, as ' \
143137
'it does not have a `.queryset` attribute.'
144138

145-
return model_cls._meta.object_name.lower()
139+
return queryset.model._meta.object_name.lower()
146140

147141
def get_routes(self, viewset):
148142
"""

tests/test_routers.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def test_urls_limited_by_lookup_value_regex(self):
180180
class TestTrailingSlashIncluded(TestCase):
181181
def setUp(self):
182182
class NoteViewSet(viewsets.ModelViewSet):
183-
model = RouterTestModel
183+
queryset = RouterTestModel.objects.all()
184184

185185
self.router = SimpleRouter()
186186
self.router.register(r'notes', NoteViewSet)
@@ -195,7 +195,7 @@ def test_urls_have_trailing_slash_by_default(self):
195195
class TestTrailingSlashRemoved(TestCase):
196196
def setUp(self):
197197
class NoteViewSet(viewsets.ModelViewSet):
198-
model = RouterTestModel
198+
queryset = RouterTestModel.objects.all()
199199

200200
self.router = SimpleRouter(trailing_slash=False)
201201
self.router.register(r'notes', NoteViewSet)
@@ -210,7 +210,8 @@ def test_urls_can_have_trailing_slash_removed(self):
210210
class TestNameableRoot(TestCase):
211211
def setUp(self):
212212
class NoteViewSet(viewsets.ModelViewSet):
213-
model = RouterTestModel
213+
queryset = RouterTestModel.objects.all()
214+
214215
self.router = DefaultRouter()
215216
self.router.root_view_name = 'nameable-root'
216217
self.router.register(r'notes', NoteViewSet)

0 commit comments

Comments
 (0)