如何在序列化后从查询中更新json数据?
16lz
2021-01-22
I have a model which have a 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)
And on views, I do a filter
query.
在视图上,我做一个过滤器查询。
comments = FessapComment.objects.filter(fessap_id=fessap_id)
And serializing.
和序列化。
json_data = serializers.serialize('json', comments)
return JsonResponse(json_data, safe=False)
As we now, here's the json look likes:
现在,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
}
]"
It's looks not cool because it just return id
of client
, I can't call the name
and address
of client
. So how to update that json in order to looks like this:
它看起来不太酷,因为它只返回客户id,我不能调用客户的名字和地址。那么如何更新这个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
}
]"
Or there's another way to call name
and address
of client
in template?
或者在模板中有另一种调用客户端名称和地址的方法?
Thank you very much for your answer...
非常感谢您的回答……
1 个解决方案
#1
1
Django has Natural Keys. Take a look at this.
Django自然键。看看这个。
更多相关文章
- 套接字错误“IP地址在其上下文中无效” - Python
- 应用Python开发WebService服务端及客户端
- python中查看变量内存地址的方法
- python模拟mysql多客户端并发操作
- linux socket网络编程:fcntl select(多个客户端连接服务器端情形)
- CentOS 7下配置IP地址
- Linux网络编程-客户端与服务器端通信(Echo)
- Linux工程实践学习笔记——基于主机系统的多客户即时通讯/聊天室
- 如果后台的SVN服务器IP地址更改了,如何修改客户端的连接url呢?