gpt4 book ai didi

python - Django休息框架: ModelSerializer as field in a ModelSerializer doesn't show choices

转载 作者:太空宇宙 更新时间:2023-11-03 17:57:12 26 4
gpt4 key购买 nike

我有两个模型:

class Article(models.Model):
name = models.CharField(max_length=255)
price = models.DecimalField(max_digits=8, decimal_places=2)

class Order(models.Model):
article = models.ForeignKey(article, related_name='orders')
bought_on = models.DateTimeField()

和两个序列化器类:

class ArticleSerializer(serializers.ModelSerializer):
class Meta:
model = Article

class OrderSerializer(serializers.ModelSerializer):
article = ArticleSerializer()

class Meta:
model = Order

现在,如果我想请求订单的选项,我不会得到“选择”,它看起来像这样:

"article": {
"type": "field",
"required": true,
"read_only": false,
"label": "Article"
},

如果我删除

article = ArticleSerializer()

从 OrderSerializer 一切正常,我获得了很多有关文章的信息,即:

"article": {
"type": "field",
"required": true,
"read_only": false,
"label": "Article",
"choices": [
{
"display_name": "Headphones - 29.00",
"value": "8"
},
{
"display_name": "Monitor- 199.00",
"value": "12"
},
]
},

这是我的问题:

是否有可能覆盖 ArticleSerializer 中的选项字段,或者是否有其他方法将 API View 中的文章显示为选项

最佳答案

这是汤姆·克里斯蒂给我的答案:

You don't want to display it as choices if it's a nested item. Rather we should be displaying it as a nested field. (We could consider that as a valid issue, tho I'd see it as pretty low priority on my own radar, so unless someone else was running with it, then...)

In terms of supporting this in your own project (rather than in core), there's some limited info on how you can go about this here... http://www.django-rest-framework.org/api-guide/metadata/ but you'd also want to dig into the existing metadata class implementation.

关于python - Django休息框架: ModelSerializer as field in a ModelSerializer doesn't show choices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28322114/

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