简体   繁体   English

如何允许用户仅在django rest框架中修改其数据?

[英]How I can allow a user to modify only their data in django rest framework?

I have a Location model that only should be modified by the owner 我有一个只能由所有者修改的位置模型

class Location(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=100)
    address = models.TextField()

This is the API Location Serializer 这是API位置序列化器

class LocationSerializer(serializers.ModelSerializer):

    class Meta:
        model = Location
        fields = ('user', 'name', 'address',)

The problem is that I want the user be the request user 问题是我希望用户成为请求用户

Actually I overwrite the create method of viewset but I think that is not the best solution 实际上我覆盖了viewset的create方法,但是我认为这不是最好的解决方案

class LocationViewSet(mixins.CreateModelMixin,
                mixins.RetrieveModelMixin,
                mixins.ListModelMixin,
                GenericViewSet):
    model = Location
    serializer_class = LocationSerializer

    def get_queryset(self):
        return self.model.objects.filter(user=self.request.user)

    def create(self, request, *args, **kwargs):
        data = request.DATA.copy()
        data.update({'user':unicode(self.request.user.id)})

        serializer = self.get_serializer(data=data, files=request.FILES)

        if serializer.is_valid():
            self.pre_save(serializer.object)
            self.object = serializer.save(force_insert=True)
            self.post_save(self.object, created=True)
            headers = self.get_success_headers(serializer.data)
            return Response(serializer.data, status=status.HTTP_201_CREATED,
                        headers=headers)

            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Thanks for any suggestions 感谢您的任何建议

You want to adapt the example from the tutorial about associating snippets with users . 您想改编教程中有关将片段与用户关联示例 Keep your get_queryset implementation and then set the user field to the request user in pre_save . 保留您的get_queryset实现,然后将user字段设置为pre_save的请求用户。

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

相关问题 如何允许用户仅在Django Rest Framework中修改他的数据 - How to allow user to modify only his data in Django Rest Framework 如何使用 django-rest-framework 让用户无法修改另一个用户的数据 - How do I not let a user be able to modify data of another user, using django-rest-framework 如何在 django REST 框架中修改 request.data - How can modify request.data in django REST framework 在django-rest-framework中,如何拥有只能由创建它的用户编辑的资源? - In django-rest-framework, how can I have a resource that is only editable by the user that created it? django rest框架,如何限制用户可以看到的数据列表? - django rest framework, how to restrict the list of data a user can see? 如何在Django Rest Framework中删除用户帐户? - How can I delete a user account in Django Rest Framework? 如何使用Django Rest Framework向用户添加汽车 - How can I add a car to a user with Django Rest Framework 在Django Rest Framework中,如何使用TemplateHTMLRenderer查看分页数据? - In Django Rest Framework, how can I view paginated data with TemplateHTMLRenderer? 仅允许在Django Rest API框架中进行更新 - Allow only update in Django rest api framework django rest框架-仅允许某些方法 - django rest framework - allow only certain methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM