gpt4 book ai didi

python - 序列化器: Customising nested relationships

转载 作者:太空宇宙 更新时间:2023-11-03 21:05:20 25 4
gpt4 key购买 nike

我有以下数据结构,类似于 serializer nested relationships example 中显示的数据结构.

型号:

class Entities(models.Model):
name = models.CharField(max_length=100, help_text="name of the object")
description = models.CharField(max_length=100, help_text="description of the object")

class EntitiesAVP(models.Model):
entity = models.ForeignKey(Entities, related_name='attributes', on_delete=models.CASCADE)
attribute = models.CharField(max_length=64, blank=False, null=True,
help_text="name of the attribute")
value = models.CharField(max_length=255, blank=False, null=True,
help_text="value of the attribute")

根据示例,我使用以下序列化程序在 API 中公开此数据:

class EntitiesAVPSerializer(serializers.ModelSerializer):
class Meta:
model = EntitiesAVP
fields = ('attribute', 'value')


class EntitiesSerializer(serializers.ModelSerializer):
attributes = EntitiesAVPSerializer(many=True, read_only=True)

class Meta:
model = Entities
fields = ('name', 'description', 'attributes')

这会以以下 JSON 结构公开我的示例数据:

[
{
"name": "Test Entity",
"description": "Description of test entity",
"attributes": [
{
"attribute": "attribute 1",
"value": "value 1"
},
{
"attribute": "attribute 2",
"value": "value 2"
},
{
"attribute": "attribute 3",
"value": "value 3"
}
]
}
]

我想要的是以以下格式呈现我的属性:

[
{
"name": "Test Entity",
"description": "Description of test entity",
"attributes": {
"attribute 1": "value 1"
"attribute 2": "value 2"
"attribute 3": "value 3"
},
]
}
]

为了尝试实现这一目标,我尝试过各种类型的相关领域。我能得到的最接近的是 CustomRelatedField ,它会生成一个字符串而不是一些 JSON。

class EntitiesAVPSerializer(serializers.RelatedField):
def to_representation(self, value):
return "{}: {}".format(value.attribute, value.value)

[
{
"name": "Test Entity",
"description": "Description of test entity",
"attributes": [
"attribute 1: value 1",
"attribute 2: value 2",
"attribute 3: value 3"
]
}
]

我对此还很陌生,感觉离我想要实现的目标并不太远。有人可以给我一点插入力,让我朝正确的方向前进吗?

编辑:

来自Hugo Luis Villalobos Canto的解决方案返回一本字典,这是一个很棒的开始,但这给了我一个字典列表而不是单个字典:

class EntitiesAVPSerializer(serializers.RelatedField):
def to_representation(self, value):
return {value.attribute: value.value}

[
{
"name": "Test Entity",
"description": "Description of test entity",
"attributes": [
{
"attribute 1": "value 1"
},
{
"attribute 2": "value 2"
},
{
"attribute 3": "value 3"
}
]
}
]

期望的输出:

[
{
"name": "Test Entity",
"description": "Description of test entity",
"attributes": {
"attribute 1": "value 1"
"attribute 2": "value 2"
"attribute 3": "value 3"
},
]
}
]

最佳答案

当然,您会得到一个字符串,这就是您从序列化器的 to_representation() 返回的内容。

你要做的就是返回一个字典,这就是你所期望的:

class EntitiesAVPSerializer(serializers.RelatedField):
def to_representation(self, value):
return {value.attribute: value.value}

编辑

如果您想要的是字典,我建议您使用SerializerMethodField,它可以让您完全控制:

class EntitiesSerializer(serializers.ModelSerializer):
attributes = serializers.SerializerMethodField()

class Meta:
model = Entities
fields = ('name', 'description', 'attributes')

def get_attributes(self, instance):
attributes_dict = {}

attributes = EntitiesAVP.objects.filter(entity=instance.entity)
for attribute in attributes:
attributes_dict[attribute.attribute] = attribute.value

return attributes_dict

关于python - 序列化器: Customising nested relationships,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55446204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com