Skip to content

Commit 477a803

Browse files
committed
Prevent empty querysets to raises AssertionError.
1 parent 0ca1145 commit 477a803

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

rest_framework/permissions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def has_permission(self, request, view):
114114
if queryset is None and getattr(view, '_ignore_model_permissions', False):
115115
return True
116116

117-
assert queryset, (
117+
assert queryset is not None, (
118118
'Cannot apply DjangoModelPermissions on a view that '
119119
'does not have `.queryset` property.'
120120
)

tests/test_permissions.py

+14
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,17 @@ class InstanceView(generics.RetrieveUpdateDestroyAPIView):
3131
authentication_classes = [authentication.BasicAuthentication]
3232
permission_classes = [permissions.DjangoModelPermissions]
3333

34+
35+
class EmptyListView(generics.ListCreateAPIView):
36+
queryset = BasicModel.objects.none()
37+
serializer_class = BasicSerializer
38+
authentication_classes = [authentication.BasicAuthentication]
39+
permission_classes = [permissions.DjangoModelPermissions]
40+
41+
3442
root_view = RootView.as_view()
3543
instance_view = InstanceView.as_view()
44+
empty_list_view = EmptyListView.as_view()
3645

3746

3847
def basic_auth_header(username, password):
@@ -149,6 +158,11 @@ def test_options_updateonly(self):
149158
self.assertIn('actions', response.data)
150159
self.assertEqual(list(response.data['actions'].keys()), ['PUT'])
151160

161+
def test_empty_view_does_not_assert(self):
162+
request = factory.get('/1', HTTP_AUTHORIZATION=self.permitted_credentials)
163+
response = empty_list_view(request, pk=1)
164+
self.assertEqual(response.status_code, status.HTTP_200_OK)
165+
152166

153167
class BasicPermModel(models.Model):
154168
text = models.CharField(max_length=100)

0 commit comments

Comments
 (0)