DRF - ModelViewSet创建 - “此字段是必需的。”

时间:2015-11-02 04:24:31

标签: django django-rest-framework

我有以下json我想发布 -

{
    "title": "test",
    "description": "testing desc",
    "username": "admin",
    "password": "Welcome1",
    "owner": 4
}

ModelViewSet位于 -

之下
class PasswordViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = Password.objects.all()
    serializer_class = PasswordSerializer
    filter_class = PasswordFilter

    def list(self, request):
        my_passwords = Password.objects.filter(owner=request.user)

        page = self.paginate_queryset(my_passwords)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = PasswordSerializer(my_passwords, many=True)
        return Response(serializer.data)

如果我删除def list,我可以做一个帖子就好了。问题是尝试在那里发布def。它返回我所有的字段都是必需的。为什么不传递数据?

这是我的序列化工具供参考 -

class PasswordSerializer(serializers.ModelSerializer):
    class Meta:
        model=Password

编辑 - 不知道在这里测试更多内容会出现什么问题,现在它甚至不能用def list删除。

模型 -

class Password(models.Model):
    title = models.CharField(max_length=100)
    description = models.CharField(max_length=1024)
    username = models.CharField(max_length=50)
    password = models.CharField(max_length=200)
    owner = models.ForeignKey('MyUser', related_name='MyUser_owner')

响应告诉我模型中的所有字段都是必需的,所以对我来说,request.data是空的,即使json应该是它。

0 个答案:

没有答案