【发布时间】:2023-03-16 18:23:01
【问题描述】:
我想用 Django 创建 REST API。我听说过 Django Rest Framework 中的序列化程序:from rest_framework import serializers。
接下来,我必须创建某种模型。
我想创建这种类型的 JSON 响应:
{
id: "489",
state: "PREPARATION",
orderDay: "2020-06-24",
deliveryDay: "2020-06-30",
deliveryAddress: "Place des Fêtes",
comment: "", // facultatif
products: [
{
id: "420",
name: "Côte de boeuf",
price: "28.90",
oldPrice: "", // facultatif
unit: "KG",
categoryId: "69666",
byProducts: [
{
id: "420161",
name: "unité",
quantity: 42,
},
],
},
],
}
所以我必须在我的序列化程序模型中添加一个列表(products、byProduct)。 我可以用序列化程序做到这一点吗?如何做到? 提前致谢
【问题讨论】:
-
你有没有尝试过的模型或序列化器?
-
尝试按照
DRF教程进行操作,它是一个出色的软件包,可以提供您正在寻找的内容。 -
看看this
标签: python django api django-rest-framework serialization