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自然键。看看这个。

更多相关文章

  1. 套接字错误“IP地址在其上下文中无效” - Python
  2. 应用Python开发WebService服务端及客户端
  3. python中查看变量内存地址的方法
  4. python模拟mysql多客户端并发操作
  5. linux socket网络编程:fcntl select(多个客户端连接服务器端情形)
  6. CentOS 7下配置IP地址
  7. Linux网络编程-客户端与服务器端通信(Echo)
  8. Linux工程实践学习笔记——基于主机系统的多客户即时通讯/聊天室
  9. 如果后台的SVN服务器IP地址更改了,如何修改客户端的连接url呢?

随机推荐

  1. 零基础学习Python列表操作
  2. 这么用 if-else,小鹿差点被辞退!
  3. centos 用户权限管理与文件权限设定 详
  4. 60 天呕心沥血,我写完这本电子书!
  5. 鹿哥带读者做外包,它香吗?
  6. TensorFlow 零基础入门指南
  7. 一文搞定pandas的数据合并
  8. 性能优化 | 实战中几点性能优化方案!
  9. 自学第五十四天
  10. 谈谈“偶像崇拜”的危害!