Prevent empty querysets to raises AssertionError.

This commit is contained in:
Nicolas Delaby 2015-04-22 10:15:34 +02:00
parent fe7cd8475b
commit 6f66798ad3
2 changed files with 14 additions and 1 deletions

View File

@ -120,7 +120,7 @@ class DjangoModelPermissions(BasePermission):
if queryset is None and getattr(view, '_ignore_model_permissions', False): if queryset is None and getattr(view, '_ignore_model_permissions', False):
return True return True
assert queryset, ( assert queryset is not None, (
'Cannot apply DjangoModelPermissions on a view that ' 'Cannot apply DjangoModelPermissions on a view that '
'does not have `.queryset` property.' 'does not have `.queryset` property.'
) )

View File

@ -41,9 +41,17 @@ class GetQuerySetListView(generics.ListCreateAPIView):
return BasicModel.objects.all() return BasicModel.objects.all()
class EmptyListView(generics.ListCreateAPIView):
queryset = BasicModel.objects.none()
serializer_class = BasicSerializer
authentication_classes = [authentication.BasicAuthentication]
permission_classes = [permissions.DjangoModelPermissions]
root_view = RootView.as_view() root_view = RootView.as_view()
instance_view = InstanceView.as_view() instance_view = InstanceView.as_view()
get_queryset_list_view = GetQuerySetListView.as_view() get_queryset_list_view = GetQuerySetListView.as_view()
empty_list_view = EmptyListView.as_view()
def basic_auth_header(username, password): def basic_auth_header(username, password):
@ -166,6 +174,11 @@ class ModelPermissionsIntegrationTests(TestCase):
self.assertIn('actions', response.data) self.assertIn('actions', response.data)
self.assertEqual(list(response.data['actions'].keys()), ['PUT']) self.assertEqual(list(response.data['actions'].keys()), ['PUT'])
def test_empty_view_does_not_assert(self):
request = factory.get('/1', HTTP_AUTHORIZATION=self.permitted_credentials)
response = empty_list_view(request, pk=1)
self.assertEqual(response.status_code, status.HTTP_200_OK)
class BasicPermModel(models.Model): class BasicPermModel(models.Model):
text = models.CharField(max_length=100) text = models.CharField(max_length=100)