gpt4 book ai didi

python - Django Rest Framework : AttributeError when Serializer many=False, 但不是当 many=True

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

我有 SlideChart 模型,幻灯片只能包含 1 个图表。序列化数据时失败并出现以下错误:

AttributeError:尝试在序列化器 ChartSerializer 上获取字段 csv 的值时出现 AttributeError。序列化程序字段的命名可能不正确,并且与 RelatedManager 实例上的任何属性或键都不匹配。原始异常文本为:“RelatedManager”对象没有属性“csv”。

但是,如果我简单地改变:

chart = ChartSerializer(many=True)

在我的 SlideSerializer(下面的代码)中它有效。这使得“图表”属性成为 JSON 中的列表,尽管它应该是一个对象,因为列表中只能有一个图表,而不是多个(或在本例中为一个)图表对象。

这是我的模型(仅相关信息的摘录):

# models.py

...

class Slide(models.Model):
TYPE_MEDIA = 'media'
TYPE_CHART = 'chart'
TYPE_CHOICES = (
(TYPE_MEDIA, 'Media'),
(TYPE_CHART, 'Chart'),
)

title = models.CharField(max_length=255,)

type = models.CharField(choices=TYPE_CHOICES, max_length=5, default=TYPE_MEDIA)

media = models.FileField(
verbose_name='media',
upload_to='slides',
null=True,
blank=True,
)

internal_only = models.BooleanField(default=False)

creation_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)


class Chart(models.Model):
slide = models.ForeignKey(Slide, related_name='chart')

csv = models.FileField(
verbose_name='csv',
upload_to='charts'
)

vertical_label = models.CharField(max_length=255,)
horizontal_label = models.CharField(max_length=255,)

creation_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)


class ChartSeriesLabel(models.Model):
"""Series label associated with a chart"""

chart = models.ForeignKey(Chart, related_name='serieslabels')

number = models.CharField(max_length=255,)
label = models.CharField(max_length=255,)

creation_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)


class ChartDataLabel(models.Model):
"""Data label associated with a chart"""

chart = models.ForeignKey(Chart, related_name='datalabels')

date = models.DateField()
label = models.CharField(max_length=255,)

creation_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)

还有我的序列化程序:

# serializers.py

...

class SeriesLabelSerializer(serializers.ModelSerializer):
class Meta:
model = ChartSeriesLabel
fields = ('number', 'label',)


class DataLabelSerializer(serializers.ModelSerializer):
class Meta:
model = ChartDataLabel
fields = ('date', 'label',)


class ChartSerializer(serializers.ModelSerializer):
serieslabels = SeriesLabelSerializer(many=True)
datalabels = DataLabelSerializer(many=True)

class Meta:
model = Chart
fields = ('csv', 'vertical_label', 'horizontal_label', 'serieslabels', 'datalabels')


class SlideSerializer(serializers.ModelSerializer):
chart = ChartSerializer()

class Meta:
model = Slide
fields = ('title', 'type', 'media', 'chart')

SlideSerializer 中使用 chart = ChartSerializer(many=True) 输出:

...

{
"title": "Slide 3",
"type": "chart",
"media": null,
"chart": [
{
"csv": "/media/commodities/Hall_of_Fame_Inductees.csv",
"vertical_label": "Vert",
"horizontal_label": "Horiz",
"serieslabels": [
{
"number": "1",
"label": "One"
},
{
"number": "2",
"label": "Two"
},
{
"number": "3",
"label": "Three"
}
],
"datalabels": [
{
"date": "2015-07-22",
"label": "This"
},
{
"date": "2015-07-23",
"label": "That"
},
{
"date": "2015-07-31",
"label": "The other"
}
]
}
]
},

...

Note that chart is a JSON list indicating that it could be many charts

SlideSerializer 中使用 chart = ChartSerializer() 的预期输出:

...

{
"title": "Slide 3",
"type": "chart",
"media": null,
"chart": {
"csv": "/media/commodities/Hall_of_Fame_Inductees.csv",
"vertical_label": "Vert",
"horizontal_label": "Horiz",
"serieslabels": [
{
"number": "1",
"label": "One"
},
{
"number": "2",
"label": "Two"
},
{
"number": "3",
"label": "Three"
}
],
"datalabels": [
{
"date": "2015-07-22",
"label": "This"
},
{
"date": "2015-07-23",
"label": "That"
},
{
"date": "2015-07-31",
"label": "The other"
}
]
}
},

...

但它会抛出上面的错误。

最佳答案

您编写的所有内容都运行良好。处理嵌套表示应该是项目列表,您应该将 many=True 标志传递给嵌套序列化程序。访问 http://www.django-rest-framework.org/api-guide/serializers/#dealing-with-nested-objects

所以你需要通过:

chart = ChartSerializer(many=True)

关于python - Django Rest Framework : AttributeError when Serializer many=False, 但不是当 many=True,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32121804/

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