Function-based views - Cannot apply DjangoModelPermissions on a view that does not set .querysetor have a.get_queryset() method
Checklist
- [x] I have verified that that issue exists against the
masterbranch of Django REST framework. - [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- [ ] This is not a usage question. (Those should be directed to the discussion group instead.)
- [x] This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
- [x] I have reduced the issue to the simplest possible case.
- [ ] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)
Steps to reproduce
Write a functional view and apply DjangoModelPermissions permissions
@api_view(['GET', 'POST', 'DELETE'])
@permission_classes((DjangoModelPermissions,))
def test(request):
if request.method == 'GET':
Expected behavior
Forbidden: /api/test
[12/Dec/2020 22:04:02] "GET /api/test HTTP/1.1" 403 63
Actual behavior
AssertionError: Cannot apply DjangoModelPermissions on a view that does not set .queryset or have a .get_queryset() method.
I don't think I'll be going down that road. Adding a queryset attribute as proposed in the associated PR will open the pandora's box to an important stream of improvements that would make the function based views bloated - such as, why being limited to a static queryset ? I'd rather keep the function based views for simpler things and advise to switch to class based view if it is not enough.
I understand, In our case someone has already written many lines of functional views and we chose to implement this rather than implementing classed based views.
Please note that api_view basically set the function into an APIView's method and the various arguments to that class attributes (see https://github.com/encode/django-rest-framework/blob/master/rest_framework/decorators.py#L23 for details).
@xordoquy,@cristiangabor ,Am having the same issue , is there a work around for it or am obliged to use CBV.