用户如何在Django Rest UpdateAPIView中更新其帖子

时间:2019-05-21 13:28:23

标签: python django django-rest-framework

这是我的序列化器类:

class CountSerializers(serializers.ModelSerializer):
    class Meta:
        model = Count
        fields = ('id','userId', 'channelId', 'rate')

这是访问视图的urls.py

path('count/<int:pk>', CountlListView.as_view()),

这是我的Views.py

class CountlListView(ListAPIView):
    serializer_class = CountSerializers
    def get_queryset(self, *args, **kwargs):
        userId = self.kwargs.get('pk')
        queryset = Count.objects.filter(userId=userId)
        print(userId)
        return queryset

如果用户ID为1,则该网址将以以下格式返回与该用户相关联的所有帖子:

[
    {
        "id": 2,                //a post id
        "userId": 1,            //user id
        "channelId": 2,         //another channeel id for which user vote
        "rate": 6               // rate(a value need to update by rest)
    },
    {
        "id": 1,
        "userId": 1,
        "channelId": 1,
        "rate": 8
    }
]

此处的id是json中的帖子ID。

现在我想将帖子ID 1中的数据(即费率值)从8更新为10。我编写了以下类和url来更新数据,但我没有这样做。这是代码:

更新api视图网址:

path('count/<int:pk>/<int:pk>/update/', CountlUpdateView.as_view()),

这是视图类:

class CountlUpdateView(UpdateAPIView):
    serializer_class = CountSerializers

    def get_queryset(self, *args, **kwargs):
        id = self.kwargs.get('pk')
        queryset = Count.objects.filter(pk=id)
        return queryset
Note:urls path('count/<int:pk>/<int:pk>/update/',CountlUpdateView.as_view()), is not correct.

0 个答案:

没有答案