gpt4 book ai didi

python - 如何修改 django-rest-framework API

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

我有一个API 。我想改变它,使它看起来像这样:

[
cats: [{
"id": 1,
"description": "I lost my mind",
"petName": "kappies",
"phone": 56765665464
},
{
"id": 2,
"description": "I lost my dog somewhere",
"petName": "Doggy",
"phone": 38093716438
}
],
dogs: [{
"id": 3,
"description": "",
"petName": "",
"phone": 0
},
{
"id": 3,
"description": "",
"petName": "",
"phone": 0
}
]
]

谁知道我该怎么做?

我需要分别为猫和狗创建API吗?

对不起,杯子。 serializers

最佳答案

假设您的模型中有 type 字段。

然后您可以在 view.py 中执行类似的操作:

posts = models.Post.objects.all()
posts_serializer = serializers.PostSerializer(posts, many=True)

output = {}
for post in posts_serializer.data:
type = output.get(post['type'], [])
output[type].append(post)

return Response(output)

您应该添加更多代码以及下次提问时尝试执行的操作。

祝你好运

关于python - 如何修改 django-rest-framework API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473759/

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