gpt4 book ai didi

python - 序列化后如何从查询更新json数据?

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

我有一个模型,它有一个 ForeignKey field 。

class Client(models.Model):
# more fields
name = models.CharField()
address = models.TextField()

class FessapComment(models.Model):
# more fields
timestamp = models.DateTimeField(auto_now=True)
client = models.ForeignKey(Client)

关于 View ,我做了一个 filter查询。

comments = FessapComment.objects.filter(fessap_id=fessap_id)

和序列化。

json_data = serializers.serialize('json', comments)
return JsonResponse(json_data, safe=False)

就像我们现在一样,json 看起来像这样:

"[
{
"fields":
{
"timestamp": "2016-05-06T13:39:46.584Z",
"client": "U2B3DBDC",
},
"model": "socmed.fessapcomment",
"pk": 1
},
{
"fields":
{
"timestamp": "2016-05-06T15:23:12.641Z",
"client": "U26A6E19",
},
"model": "socmed.fessapcomment",
"pk": 2
}
]"

它看起来不太酷,因为它只返回 idclient ,我无法调用 nameaddressclient .那么如何更新该 json 以使其看起来像这样:

"[
{
"fields":
{
"timestamp": "2016-05-06T13:39:46.584Z",
"client":
{
"id": "U2B3DBDC",
"name": "Herman James",
"address": "Uooepi St.",
},
},
"model": "socmed.fessapcomment",
"pk": 1
},
{
"fields":
{
"timestamp": "2016-05-06T15:23:12.641Z",
"client":
{
"id": "U26A6E19",
"name": "Jared",
"address": "Ter St.",
},
},
"model": "socmed.fessapcomment",
"pk": 2
}
]"

或者有另一种方式调用nameaddressclient在模板中?

非常感谢您的回答...

最佳答案

Django 有 Natural Keys .看看这个。

关于python - 序列化后如何从查询更新json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37084738/

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