Django REST Framework嵌套资源密钥“ id”;无法进入 [英] Django REST Framework nested resource key "id" unaccessible

查看:56
本文介绍了Django REST Framework嵌套资源密钥“ id”;无法进入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我具有以下结构:

So I have the following Structure:

ClientFile属于所有者(类名=联系人)。
我正在尝试使用API​​创建Clientfile。该请求包含以下数据:

A ClientFile belongs to an Owner (class name = Contact). I'm trying to create a Clientfile using the API. The request contains the following data:

{
  name: "Hello!"
  owner: {
    id: 1,
    first_name: "Charlie",
    last_name: "Watson"
  }
}

我根据自己的结构创建了序列化器。希望此API调用将创建一个名称为 Hello!的客户端文件。并以联系人ID 1作为所有者:

I created the serializer according to my structure. Hoping that this API call would create a clientfile with the name "Hello!" and Contact id 1 as the owner:

class ContactSerializer(serializers.ModelSerializer):
  class Meta:
    model = Contact
    fields = (
      'id',
      'first_name',
      'last_name',
    )

class ClientfileSerializer(serializers.ModelSerializer):

  owner = ContactSerializer(read_only=False)

  class Meta():
    model = Clientfile
    fields = (
      'id',
      'name',
      'owner',
    )

  def create(self, validated_data):

    owner = Contact.objects.get(pk=validated_data['owner']['id'])

我确实进入了create方法。但是,我需要的唯一字段(['owner'] ['id'])无法访问。如果我执行 print ['owner'] ['first_name'] ,它将返回'Charlie'。但是由于某些原因,该ID似乎无法访问...

I do get into the create method. However, the only field I need (['owner']['id']) is not accessible. If I do print ['owner']['first_name'] it does return 'Charlie'. But the ID for some reasons doesn't seem to be accessible...

为什么会发生这种情况?我想念什么吗? (我是Django的新手)

Any reasons why this can be happening? Am i missing something? (I'm new to Django)

解决方案:刚刚发现ID不在其中显示的原因第一名是因为我必须在这样的字段中声明它:希望对您有所帮助。

SOLUTION: Just found out that the reason why ID didn't show in the first place was because I had to declare it in the fields like so: Hope this helps.

class ContactSerializer(serializers.ModelSerializer):

  id = serializers.IntegerField() # ← Here

  class Meta:
    model = Contact
    fields = (
      'id',
      'first_name',
      'last_name',
    )


推荐答案

好的,所以我发现了另一种可行的方法。
我为所有者关系添加了IntegerField序列化程序。我还必须将所有者关系设置为read_only = True。

Alright so I found a different approach that works. I added an IntegerField serializer for the owner relation. I also had to set the owner relation to read_only=True.

这是我通过POST发送的json:

This is the json I am sending via POST:

{
  name: "Hello!"
  owner_id: 1
}

这是我的序列化器:

class ClientfileSerializer(serializers.ModelSerializer):

  owner_id = serializers.IntegerField()
  owner = ContactSerializer(read_only=True)

  class Meta():
    model = Clientfile
    fields = (
      'id',
      'owner_id',
      'owner',
    )

似乎比第一种方法还不错,但是它做的工作。
Plus我不想创建新的所有者,只需选择数据库中已经存在的一个。因此,也许只有ID而不是通过Json发布的全部信息更具语义。

It seems less cool than the first way, but it does the job. Plus I don't want to create a new owner, but just select one that is already in the database. So maybe it's more semantic to only have the ID and not the full set of information posted via Json.

这篇关于Django REST Framework嵌套资源密钥“ id”;无法进入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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