Returning custom message when a permission is denied in DRF

前端 未结 7 1957
盖世英雄少女心
盖世英雄少女心 2021-02-13 01:20

Django REST Framework has an excellent piece of documentation about permissions. I\'ve been able to use pre-made permission classes and also built my own.

However, there

7条回答
  •  粉色の甜心
    2021-02-13 02:00

    Building on Aysennoussi’s answer:

    from rest_framework import permissions
    From django.utils import timezone
    
    class CustomerAccessPermission(permissions.BasePermission):
        message = 'Adding customers not allowed.'
    
        def has_permission(self, request, view): 
            if request.user.has_expired:
                self.message = “Your account has expired.”
                return False
            elif request.user.has_access:
                return True
            else:
                return False
    
    

提交回复
热议问题