mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 04:50:12 +03:00
Merge 742231f450
into 69b0ac52c2
This commit is contained in:
commit
bbc2584886
|
@ -206,6 +206,22 @@ Custom permissions will raise a `PermissionDenied` exception if the test fails.
|
||||||
def has_permission(self, request, view):
|
def has_permission(self, request, view):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
|
If you want to have more control over the error message, for example, format with data, fetched from the database, you can define a `message` property in the permission class:
|
||||||
|
|
||||||
|
from rest_framework import permissions
|
||||||
|
|
||||||
|
class CustomerAccessPermission(permissions.BasePermission):
|
||||||
|
@property
|
||||||
|
def message(self):
|
||||||
|
product = SomeProductModel.objects.get(...)
|
||||||
|
return 'Adding customers for product {} not allowed.'.format(product.name)
|
||||||
|
|
||||||
|
def has_permission(self, request, view):
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
The following is an example of a permission class that checks the incoming request's IP address against a blacklist, and denies the request if the IP has been blacklisted.
|
The following is an example of a permission class that checks the incoming request's IP address against a blacklist, and denies the request if the IP has been blacklisted.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user