mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-01 02:50:06 +03:00
Fix the way DjangoModelPermissions asks for the model to be checked.
This commit is contained in:
parent
bf9533ae37
commit
fbef9ca9e4
|
@ -184,13 +184,15 @@ class DjangoModelPermissions(BasePermission):
|
|||
'`.queryset` or have a `.get_queryset()` method.'
|
||||
).format(self.__class__.__name__)
|
||||
|
||||
if hasattr(view, 'get_queryset'):
|
||||
queryset = getattr(view, 'queryset', None)
|
||||
|
||||
if queryset is None:
|
||||
queryset = view.get_queryset()
|
||||
assert queryset is not None, (
|
||||
'{}.get_queryset() returned None'.format(view.__class__.__name__)
|
||||
'The value of {0}.queryset and {0}.get_queryset() is None.'.format(view.__class__.__name__)
|
||||
)
|
||||
return queryset
|
||||
return view.queryset
|
||||
|
||||
return queryset
|
||||
|
||||
def has_permission(self, request, view):
|
||||
# Workaround to ensure DjangoModelPermissions are not applied
|
||||
|
|
|
@ -236,12 +236,14 @@ class ModelPermissionsIntegrationTests(TestCase):
|
|||
|
||||
# Faulty `get_queryset()` methods should trigger the above "view does not have a queryset" assertion.
|
||||
class View(RootView):
|
||||
queryset = None
|
||||
|
||||
def get_queryset(self):
|
||||
return None
|
||||
view = View.as_view()
|
||||
|
||||
request = factory.get('/', HTTP_AUTHORIZATION=self.permitted_credentials)
|
||||
with self.assertRaisesMessage(AssertionError, 'View.get_queryset() returned None'):
|
||||
with self.assertRaisesMessage(AssertionError, 'The value of View.queryset and View.get_queryset() is None.'):
|
||||
view(request)
|
||||
|
||||
|
||||
|
@ -589,3 +591,4 @@ class PermissionsCompositionTests(TestCase):
|
|||
permissions.IsAuthenticated
|
||||
)
|
||||
assert composed_perm().has_permission(request, None) is True
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user