简体   繁体   English

Django Restframework has_object_permission()函数不适用于对象权限

[英]Django Restframework has_object_permission() function is not working for object permission

I'm in the process of debugging my custom permissions class and returning a value of False for my has_object_permission() function, but my I'm still able to access my API (GET request), via Restframework's API browser without authenticating and I can't understand why. 我正在调试自定义权限类并为has_object_permission()函数返回False值,但是我仍然可以通过Restframework的API浏览器访问我的API(GET请求),而无需进行身份验证,我可以不明白为什么。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Please see code below. 请参见下面的代码。 for whatever reasons, it appears that my has_object_permission function is not executing. 无论出于什么原因,看来我的has_object_permission函数没有执行。 Please Help 请帮忙

urls.py urls.py

router = BulkRouter()
router.register(r'api1', SimpleViewSet1)


urlpatterns = [
    url(r'^test/', include(router.urls, namespace='api1')),
]

views.py views.py

class SimpleViewSet1(generics.BulkModelViewSet):    
    queryset = Barcode.objects.all()
    permission_classes = (MyUserPermission,)
    serializer_class = SimpleSerializer1


    def get_queryset(self):
        user = User.objects.get(pk=2)
        return Barcode.objects.filter(owner = user)


    def get_object(self):
        obj = get_object_or_404(self.get_queryset())
        self.check_object_permissions(self.request, obj)
        return obj

permissions.py Permissions.py

class MyUserPermission(BasePermission):

    def has_permission(self, request, view):
        return True


    def has_object_permission(self, request, view, obj):
        return False

serializer.py 序列化器

class SimpleSerializer1(BulkSerializerMixin,  # only required in DRF3
                       ModelSerializer):

    owner = serializers.ReadOnlyField(source='owner.username')

    class Meta(object):
        model = Barcode
        # only required in DRF3
        list_serializer_class = BulkListSerializer
        fields = ('barcode_number', 'barcode_type', 'owner')

models.py models.py

@python_2_unicode_compatible
class Barcode(models.Model):
    owner = models.ForeignKey('auth.User', related_name = 'barcodes')
    barcode_number = models.CharField(max_length=200)
    barcode_type = models.CharField(max_length=200)

    def __str__(self):
        return self.barcode_number

Django Rest API Guide says : Django Rest API指南说

Also note that the generic views will only check the object-level permissions for views that retrieve a single model instance. 还要注意,通用视图将仅检查对象级权限以获取检索单个模型实例的视图。 If you require object-level filtering of list views, you'll need to filter the queryset separately. 如果需要列表视图的对象级过滤,则需要分别过滤查询集。 See the filtering documentation for more details. 有关更多详细信息,请参见过滤文档

rest_framework.generics.BulkModelViewSet, as it's name suggests,does bulk operations. 顾名思义,rest_framework.generics.BulkModelViewSet进行批量操作。 It means that you have to use object-level filtering as proposed in the docs. 这意味着您必须按照文档中的建议使用对象级过滤。

You should be looking especially under this section. 您应该在节中特别关注。 Pay close attention to the example and make use of the code. 请密切注意该示例并利用代码。 You should also read about the DjangoModelPermissions to understand how does the example in the link above works. 您还应该阅读DjangoModelPermissions,以了解上面链接中的示例如何工作。

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

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