Django REST Framework不同深度的POST / PUT? [英] Django REST Framework different depth for POST/PUT?

查看:1057
本文介绍了Django REST Framework不同深度的POST / PUT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django REST框架为我的网络应用程序创建一个API。我有一个类'Comment',它在 Meta 类中设置 depth = 2 GET 评论。当我尝试发送一个 POST PUT 请求(即创建一个新的注释)我被告知我需要包含对象而不是ForeignKey ID。



这是我的Serializer类:

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

模型:

 类注释(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)

视图代码:

 类注释(generics.ListCreateAPIView):

model =注释
serializer_class = CommentSerializer

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

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

  {user:[此字段为必填项。],budget:[{non_field_errors :[无效数据]}]} 

发送此原始数据时:

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


解决方案

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



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



我可以想象,一个 PrimaryKeyRelatedField serializer将正确处理JSON数据提交,就像您在示例中使用的那样。 p>

I am using Django REST Framework to create an API for my web app. I have a class 'Comment', that has depth=2 set in the Meta class. This works great when GETing the Comments. When I try to send a POST or PUT request though (i.e. create a new Comment) I am told I need to include objects instead of ForeignKey IDs.

Here's my Serializer class:

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

The model:

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)

The view code:

class Comments(generics.ListCreateAPIView):

    model = Comment
    serializer_class = CommentSerializer

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

And the error that is displayed in the output (JSON) is:

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

When this raw data is sent:

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

解决方案

I believe the proper way to define a serializer field that refers to a foreign key relationship is through something like serializers.PrimaryKeyRelatedField. I don't believe that model serializers automatically use this field class without defining it explicitly in the serializer class.

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

I would imagine that a PrimaryKeyRelatedField serializer would correctly handle JSON data submissions like the one you used in your example.

这篇关于Django REST Framework不同深度的POST / PUT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆