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...
非常感谢您的回答……