在Django Rest Framework中发布外键关系

dar*_*rse 9 django django-rest-framework

在我的模型中,我有以下类:

class Topic(models.Model):
    name = models.CharField(max_length=25, unique=True)

class Content(models.Model):
    title = models.CharField(max_length=255)
    body = models.TextField()
    topic = models.ForeignKey(Topic, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)

我的序列化器是这样的:

class TopicSerializer(serializers.ModelSerializer):
     class Meta:
         model = Topic
         fields = ('name')

class ContentSerializer(serializers.ModelSerializer):
     topic = TopicSerializer(read_only=True)
     class Meta:
          model = Content
          fields = ('title', 'body', 'topic')
Run Code Online (Sandbox Code Playgroud)

好吧,所以在我的网址文件中,我有以下模式:

urlpatterns = [
    ...
    url(r'^api/topic_detail/(?P<name>[a-zA-Z0-9-]+)/content_list/$', views.topic_content_list, name='topic_content_list'),
    ...
]
Run Code Online (Sandbox Code Playgroud)

因此,当用户说/api/topic_detail/sports/content_list/,我们获得具有体育主题的所有内容的列表.现在我想的是,如果我们POST以下数据到上述网址,那么内容对象是否使用自动与体育有关的话题场创建.

我试图在我的观点中以下列方式执行此操作:

@api_view(['GET', 'POST'])
def topic_content_list(request, name):
    try:
        topic = Topic.objects.get(name=name)
    except:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        contents = Content.objects.filter(topic=topic)
        serializer = ContentSerializer(contents, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        request.data["topic"] = topic
        serializer = ContentSerializer(data=request.data)
        print request.data
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Run Code Online (Sandbox Code Playgroud)

现在让我说我去URL /api/topic_detail/sports/content_list/并发布:

{
    "title": "My blog post",
    "body" : ".....",
}
Run Code Online (Sandbox Code Playgroud)

将创建内容对象,并正确设置标题和正文字段.但是,topic字段设置为null.我怎样才能解决这个问题?任何帮助表示赞赏.

此外,请不要建议使用通用视图集,因为我对自动发生的这么多事情感到不舒服.

编辑

好吧,所以我修正了我的愚蠢错误:

class ContentSerializer(serializers.ModelSerializer):
     topic = TopicSerializer(read_only=False)
     class Meta:
          model = Content
          fields = ('title', 'body', 'topic')
Run Code Online (Sandbox Code Playgroud)

也就是说,我将read_only参数设置为False.但是,现在该帖子会创建一个新错误:

{
    "topic": {
        "non_field_errors": [
            "Invalid data. Expected a dictionary, but got Topic."
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

我很确定这是指我发送的data.website不是JSON,而只是一个Django模型实例.我如何JSONify单个实例?

Vin*_*mar 8

这是来自您的序列化程序.

     topic = TopicSerializer(read_only=True)
Run Code Online (Sandbox Code Playgroud)

这意味着您的主题是只读的,因此当您尝试保存序列化程序时,主题不会被保存.删除它,问题将得到解决.

编辑:

现在按照第二个错误,这是因为它正在期待一个dict并且你正在传递模型实例,所以你有两个选择.要么手动创建字典.

topic_dict = { "name": topic.name }

并将其作为'topic'传递给request.data,然后保存或给出topic_id,因为有一个外键关系,它应该工作.

所以它会是这样的:

request.data["topic_id"] = topic.id
Run Code Online (Sandbox Code Playgroud)

现在你选择做什么完全取决于你.

  • 有趣.如果我传入字典,手动设置主题字段,例如:topic.id和topic.name,那么我会收到一条错误消息,指出已存在具有此名称的主题.换句话说,使用此方法会尝试创建新主题,而不是将其与现有主题相关联. (3认同)