[英]Django Rest Framework serializer doesn't get CharField
我已经使用 Django 和 Django Rest Framework 构建了一个 API。 在我的串行我定义的organisation
可以发布,但需要被存储到不同的模式。 我定义了我的序列化程序如下:
class DeviceSerializer(serializers.HyperlinkedModelSerializer):
geometrie = PointField(required=False)
organisation = serializers.CharField(source='owner.organisation')
owner = PersonSerializer(required=False)
class Meta:
model = Device
fields = (
'id',
'geometrie',
'longitude',
'latitude',
'organisation',
'owner',
)
def get_longitude(self, obj):
if obj.geometrie:
return obj.geometrie.x
def get_latitude(self, obj):
if obj.geometrie:
return obj.geometrie.y
def create(self, validated_data):
print("ORG:", validated_data.get('organisation', "NO ORG FOUND")) #
# Do some custom logic with the organisation here
但是当我向它发布一些 json 时,其中包括一个organisation
(我对输入进行了三次检查),它会打印一行ORG: NO ORG FOUND
。
为什么它不转发组织?
[编辑]
型号代码:
class Person(models.Model):
name = models.CharField(max_length=255)
email = models.EmailField()
organisation = models.CharField(max_length=250, null=True, blank=True)
class Device(models.Model):
geometrie = gis_models.PointField(name='geometrie', null=True, blank=True)
owner = models.ForeignKey(to='Person', on_delete=models.SET_NULL, null=True, blank=True, related_name='owner')
和测试代码:
def test_full_post(self):
device_input = {
"geometrie": {"longitude": 4.58565, "latitude": 52.0356},
"organisation": "Administration."
}
url = reverse('device-list')
self.client.force_login(self.authorized_user)
response = self.client.post(url, data=device_input, format='json')
self.client.logout()
尝试更改行:
print("ORG:", validated_data.get('organisation'], "NO ORG FOUND"))
对此:
print("ORG:", validated_data.get('organisation', "NO ORG FOUND"))
由于添加了source
参数,DRF 会自动将organisation
数据推送到嵌套级别
因此,如果您想访问组织数据,请尝试以下操作,
class DeviceSerializer(serializers.HyperlinkedModelSerializer):
organisation = serializers.CharField(source='owner.organisation')
# other code stuffs
def create(self, validated_data):
organisation = validated_data['owner']['organisation'] print("ORG:", organisation)
请尝试“StringRelatedField”。
class DeviceSerializer(serializers.HyperlinkedModelSerializer):
geometrie = PointField(required=False)
organisation = serializers.CharField(source='owner.organisation') # yours
# ----------------
organisation = serializers.StringRelatedField(source='owner.organisation') # try this.
owner = PersonSerializer(required=False)
# your code
由于您使用的是带点符号的序列化器字段, validated_data
将是:
{
'geometrie': {'longitude': 4.58565, 'latitude': 52.0356},
'owner': {'organisation': 'Administration.'}
}
因此,您可以作为validated_data['owner']['organisation']
访问组织
但是,如果您想序列化另一个相关表/外键的属性/列,您需要使用StringRelatedField
[ organisation = serializers.StringRelatedField(source='owner.organisation')
] 这将确保instance fetched from the database contains the proper
“设备instance fetched from the database contains the proper
attribute during a
GET` 请求attribute during a
instance fetched from the database contains the proper
组织attribute during a
。
但是反序列化不起作用,您需要在create
方法中进行额外的实现。 这是因为您需要创建一个Person
实例(与organisation
),然后将Device
实例连接到该新创建的实例。
一个具体的例子可以在这里找到: 编写嵌套序列化程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.