简体   繁体   English

Django Rest Framework ListCreateAPIView是否不检查has_object_permissions?

[英]Django Rest Framework ListCreateAPIView not checking has_object_permissions?

I've been trying to figure this out for almost a full day now, and I can't seem to figure out why has_object_permission method isn't called when the ListCreateAPIView is called in DRF . 我已经尝试解决了整整一天的时间,而且似乎无法弄清楚为什么在DRF调用ListCreateAPIView时没有调用has_object_permission method I've tried all the solutions I could find, but according to the docs check_object_permissions is called in this class already. 我已经尝试了所有可以找到的解决方案,但是根据文档check_object_permissions已经在此类中被调用。

I know it has to be something stupid I'm missing. 我知道这一定是我想念的愚蠢之举。 Code snippets are below, please help! 下面是代码段,请帮忙!

views.py: views.py:

from accountability.models import AccountabilityItem
from accountability.serializers import AccountabilityItemSerializer
from rest_framework import generics
from .permissions import InGroup

class AccountabilityItemListCreate(generics.ListCreateAPIView):
    queryset = AccountabilityItem.objects.all()
    serializer_class = AccountabilityItemSerializer
    permission_classes = (InGroup,)

permissions.py: Permissions.py:

from rest_framework import permissions


class InGroup(permissions.BasePermission):
    """
    Custom permission to only allow owners of an object to edit it.
    """
    def has_object_permission(self, request, view, obj):
        print('Checking for object')
        return False

Another note, I've added the has_permission method to the permissions.py file, and this method runs all the time no matter what. 另一个注意事项是,我已经将has_permission方法添加到permissions.py文件中,并且无论何时该方法始终运行。

Thanks! 谢谢!

Calling has_object_permission doesn't make sense for lists. 调用has_object_permission对列表没有意义。 It is intended for single instances. 它适用于单个实例。

What you want is to filter your list of objects so it only leaves those for which the user has some permissions. 您想要的是过滤对象列表,以便仅保留用户具有某些权限的那些对象。 DjangoObjectPermissionsFilter does it but requires django-guardian. DjangoObjectPermissionsFilter可以,但是需要django-guardian。 You might get a similar result but creating your own filtering class ( sources for DjangoObjectPermissionsFilter ) 您可能会得到类似的结果,但是创建自己的过滤类( DjangoObjectPermissionsFilter的源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM