37π
LATER EDIT
As it seems that DRF decorators donβt really work (at least not for me), this is the best solution I could come up with:
def get_permissions(self):
# Your logic should be all here
if self.request.method == 'GET':
self.permission_classes = [DummyPermission, ]
else:
self.permission_classes = [IsAuthenticated, ]
return super(UsersViewSet, self).get_permissions()
This actually works for both cases that you asked, but requires a bit more work. However, Iβve tested it and it does the job.
ORIGINAL ANSWER BELOW
There is a small mistake in the docs, you should be sending a list to the decorator (not a tuple). So it should be like this:
@permission_classes([IsAuthenticated, AdditionalPermission, ])
def update:
pass
To answer your questions:
how can I add additional Permission only for update method?
First of all, you should know that DRF first checks for global permissions (those from the settings file), then for view permissions (declared in permission_classes β if these exist, they will override global permissions) and only after that for method permissions (declared with the decorator @permission_classes). So another way to do the above is like this:
@permission_classes([AdditionalPermission, ])
def update:
pass
Since ISAuthenticated is already set on the entire view, it will always be checked BEFORE any other permission.
overwrite permissions only for update method?
Well, this is hard(er), but not impossible. You can:
- set the permissions for each method and remove it from the class
- modify your AdditionalPermission class so that it also checks for user authentication if the method is not update.
Good luck.
12π
You can also specify permissions for specific methods in the get_permissions() method:
class MyViewSet(viewsets.ModelViewSet):
def get_permissions(self):
if self.action in ('update', 'other_viewset_method'):
self.permission_classes = [permissions.CustomPermissions,]
return super(self.__class__, self).get_permissions()
- How to make a rest_framework Serializer disallow superfluous fields?
- Django syncdb and migrate
- Annotate django query if filtered row exists in second table
- What is difference between instance namespace and application namespace in django urls?
- Host Django with XAMPP on Windows
2π
@permission_classes didnβt work for class based view. And I tried @detail_route(permission_classes=(permissions.CustomPermissions,)) for update view function, still not work.
so, my solution is:
class MyViewSet(viewsets.ModelViewSet):
def update(self, request, *args, **kwargs):
self.methods=('put',)
self.permission_classes = (permissions.CustomPermissions,)
return super(self.__class__, self).update(request, *args, **kwargs)
Have a try. my drf is v3.1.1
- PyCharm, Django: zero code coverage
- Django fixture fails, stating "DatabaseError: value too long for type character varying(50)"
- Docker compose could not open directory permisson denied
1π
For me, get_permissions worked but it did turn out that if you sending in Authorization in your header in your request rest framework will throw an error even if permission is set to AllowAny. If you are going to use both authorization and AllowAny you need to have to take this into consideration.
- Django: 'python manage.py migrate' taking hours (and other weird behavior)
- Could not import 'oauth2_provider.ext.rest_framework.OAuth2Authentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'
- Django β How to save m2m data via post_save signal?
- How can filter parent based on children in django
0π
Yes you can by adding annotation
See this link for more information there are examples:
- Django β forms.FileField() initial value
- Django β 'WhereNode' object has no attribute 'output_field' error
- How to override Django admin's views?
- Django/celery β celery status: Error: No nodes replied within time constraint