Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何限制用户在DRF ModelViewSet中看不到其他用户数据?_Python_Django_Python 3.x_Django Rest Framework - Fatal编程技术网

Python 如何限制用户在DRF ModelViewSet中看不到其他用户数据?

Python 如何限制用户在DRF ModelViewSet中看不到其他用户数据?,python,django,python-3.x,django-rest-framework,Python,Django,Python 3.x,Django Rest Framework,我创建了一个todo列表API,其中包含用户配置文件。每个用户配置文件都可以有待办事项列表。现在一切正常,但这里的问题是一个用户可以看到其他用户的数据,但我想限制一个用户在请求URL时不查看其他用户的待办事项列表 models.py views.py 类TodoItemViewSet(ViewSet.ModelViewSet): “”“处理创建、读取和更新配置文件待办事项。”“” 身份验证\类=(令牌身份验证) serializer\u class=serializers.TodoItemSer

我创建了一个todo列表API,其中包含用户配置文件。每个用户配置文件都可以有待办事项列表。现在一切正常,但这里的问题是一个用户可以看到其他用户的数据,但我想限制一个用户在请求URL时不查看其他用户的待办事项列表

models.py views.py
类TodoItemViewSet(ViewSet.ModelViewSet):
“”“处理创建、读取和更新配置文件待办事项。”“”
身份验证\类=(令牌身份验证)
serializer\u class=serializers.TodoItemSerializer
queryset=models.TodoListItem.objects.all()
权限\类=(permissions.UpdateTodoItem,已验证)
def执行_创建(自、序列化程序):
“”“将用户配置文件设置为登录用户。”“”
serializer.save(user\u profile=self.request.user)
序列化程序.py
类TodoItemSerializer(serializers.ModelSerializer):
“”“Todo项目的序列化程序。”“”
类元:
model=models.todolitItem
字段=('id'、'user\u profile'、'todo\u item'、'description'、'created\u on'、'提醒日期')
extra_kwargs={'user_profile':{'read_only':True}
权限.py
class UpdateTodoItem(permissions.BasePermission):
“”“允许用户更新自己的状态。”“”
def具有对象权限(自我、请求、查看、obj):
“”“检查用户是否正在尝试更新自己的状态。”“”
如果permissions.SAFE_方法中的request.method:
返回真值
返回obj.user_profile.id==request.user.id
意外结果: 预期结果:
我必须只查看用户配置文件1的todo项目,因为用户配置文件:1是已登录的用户。

您可以尝试此操作。返回特定用户的记录

class TodoItemViewSet(viewsets.ModelViewSet):
    """Handles creating, reading, and updating profile Todo Items."""

    authentication_classes = (TokenAuthentication,)
    serializer_class = serializers.TodoItemSerializer
    queryset = models.TodoListItem.objects.all()
    permission_classes = (permissions.UpdateTodoItem, IsAuthenticated)

    def perform_create(self, serializer):
        """Sets the user profile to the logged in User."""
        serializer.save(user_profile=self.request.user)

    def get_queryset(self):
        return self.queryset.filter(user_profile=self.request.user)
希望能有帮助

提及

[
    {
        "id": 1,
        "user_profile": 1,
        "todo_item": "Todo Item 1",
        "description": "Sample todo item 1",
        "created_on": "2019-06-06T04:48:59.401451Z",
        "reminder_date": "2019-06-02T04:48:57Z"
    },
    {
        "id": 2,
        "user_profile": 2,
        "todo_item": "Todo Item 2",
        "description": "Sample todo item 3",
        "created_on": "2019-06-06T04:50:08.734365Z",
        "reminder_date": "2019-06-03T04:50:07Z"
    },
    {
        "id": 3,
        "user_profile": 1,
        "todo_item": "Todo Item 2",
        "description": "",
        "created_on": "2019-06-06T04:54:47.919602Z",
        "reminder_date": "2019-06-07T02:00:00Z"
    },
    {
        "id": 4,
        "user_profile": 1,
        "todo_item": "Todo Item 4",
        "description": "Sample todo item 4",
        "created_on": "2019-06-06T05:00:08.004224Z",
        "reminder_date": "2019-06-07T10:01:00Z"
    }
]
[
    {
        "id": 1,
        "user_profile": 1,
        "todo_item": "Todo Item 1",
        "description": "Sample todo item 1",
        "created_on": "2019-06-06T04:48:59.401451Z",
        "reminder_date": "2019-06-02T04:48:57Z"
    },
    {
        "id": 3,
        "user_profile": 1,
        "todo_item": "Todo Item 2",
        "description": "",
        "created_on": "2019-06-06T04:54:47.919602Z",
        "reminder_date": "2019-06-07T02:00:00Z"
    },
    {
        "id": 4,
        "user_profile": 1,
        "todo_item": "Todo Item 4",
        "description": "Sample todo item 4",
        "created_on": "2019-06-06T05:00:08.004224Z",
        "reminder_date": "2019-06-07T10:01:00Z"
    },
]
class TodoItemViewSet(viewsets.ModelViewSet):
    """Handles creating, reading, and updating profile Todo Items."""

    authentication_classes = (TokenAuthentication,)
    serializer_class = serializers.TodoItemSerializer
    queryset = models.TodoListItem.objects.all()
    permission_classes = (permissions.UpdateTodoItem, IsAuthenticated)

    def perform_create(self, serializer):
        """Sets the user profile to the logged in User."""
        serializer.save(user_profile=self.request.user)

    def get_queryset(self):
        return self.queryset.filter(user_profile=self.request.user)