python - Custom permission return always true, django -
sorry english. learning django rest , try create custom permisssion user. fox example permission: "big" , "small".
my user model
class user(abstractuser): is_active = models.booleanfield(default=false) email = models.emailfield(blank=false, unique=true) avatar = models.imagefield(upload_to='avatar/users', blank=true) class meta: permissions = ( ("big", "this big boy"), ("small", "this small"), )
then added permission user this:
permission = permission.objects.get(name='big') user.user_permissions.add(permission) user.save()
created custom class, class check permission:
class isbigboy(permissions.basepermission): def has_permission(self, request, view): return request.user.has_perm('users.big')
and in view create test class check custom permission
class mysupertestrequest(generics.createapiview): permission_classes = (permissions.isauthenticated, custom_permissions.isbigboy) serializer_class = createsomethingserializer def post(self, return response({"detail": "good"}, status.http_200_ok)
problem:
return request.user.has_perm('users.big')
it code return true
may need user has_perms()
logic, , don't forget if user is_superuser has permissions.
Comments
Post a Comment