11

我正在使用 Django REST Framework 为我的 Web 应用程序创建 API。我有一个班级“评论”,已depth=2Meta班级中设置。GET这在ing时效果很好Comments。当我尝试发送POSTorPUT请求时(即创建一个新的Comment),我被告知我需要包含对象而不是 ForeignKey ID。

这是我的序列化程序类:

class CommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment
        depth = 2 

该模型:

class Comment(models.Model):
    user = models.ForeignKey(User, null=True, blank=True,
        related_name='comments')
    budget = models.ForeignKey(Budget, related_name='comments')
    published = models.BooleanField(default=False)
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)

视图代码:

class Comments(generics.ListCreateAPIView):

    model = Comment
    serializer_class = CommentSerializer

    def pre_save(self, obj):
        obj.user = self.request.user

输出(JSON)中显示的错误是:

{"user": ["This field is required."], "budget": [{"non_field_errors": ["Invalid data"]}]}

发送此原始数据时:

{"budget": 2, "published": true, "body": "Another comment"}
4

4 回答 4

8

我知道这有点晚了,但我最终使用了 2 个序列化器,如下所示:

class CommentReadSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment
        depth = 2

class CommentWriteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment

然后像这样使用:

class Comments(generics.ListCreateAPIView):

    model = Comment
    serializer_class = CommentReadSerializer

    def create(self, request, *args, **kwargs):
        serializer = CommentWriteSerializer(data=request.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)
            serializer = CommentReadSerializer(serializer.object)
            return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
于 2014-11-04T17:13:06.957 回答
7

您可以通过覆盖该get_serializer_class()函数来设置不同的序列化程序,如下所示:

def get_serializer_class(self): method = self.request.method if method == 'PUT' or method == 'POST': return YourWriteSerializer else: return YourReadSerializer

我想添加这个,因为一段时间后我从谷歌搜索来到这里。

于 2017-08-08T12:49:11.630 回答
6

我相信定义引用外键关系的序列化器字段的正确方法是通过类似serializers.PrimaryKeyRelatedField. 我不相信模型序列化程序会自动使用此字段类,而无需在序列化程序类中明确定义它。

http://www.django-rest-framework.org/api-guide/relations/#primarykeyrelatedfield

我想一个PrimaryKeyRelatedField序列化程序会像您在示例中使用的那样正确处理 JSON 数据提交。

于 2013-07-13T02:51:27.913 回答
3

我有同样的问题,所以我解决了制作自定义通用方法。这是上述答案的更好实现

class CustomListCreateAPIView(mixins.ListModelMixin,
                              mixins.CreateModelMixin,
                              generics.GenericAPIView):
    """
    Concrete view for listing a queryset or creating a model instance.
    """
    def get_serializer_class(self):
        method = self.request.method
        if method == 'PUT' or method == 'POST':
            return self.writeSerializers
        else:
            return self.readSerializers

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

同样的路德,

class CustomRetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
                                         mixins.UpdateModelMixin,
                                         mixins.DestroyModelMixin,
                                         generics.GenericAPIView):
    """
    Concrete view for retrieving, updating or deleting a model instance.
    """
    def get_serializer_class(self):
        method = self.request.method
        if method == 'PUT' or method == 'POST':
            return self.writeSerializers
        else:
            return self.readSerializers

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def patch(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)

    def delete(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)  # enter code here

现在我只在 Views.py 中给出writeSerializersreadSerializers

还有一种创建读写序列化器的简单方法。

class employeeWriteSerializer(serializers.ModelSerializer):
    class Meta:
        model = employee
        fields = ('username','email',..)
        
class employeeReadSerializer(serializers.ModelSerializer):
     class Meta(employeeWriteSerializer.Meta):
         depth = 1

它可以节省时间和重复工作,您还可以在自定义通用 Api(Retitve 工作)中添加身份验证类。谢谢。

于 2021-12-04T18:43:07.420 回答