【发布时间】:2019-01-09 15:21:48
【问题描述】:
假设我有一个名为 Blog 的模型,这就是该值的工作方式
>>> Blog.objects.values()
[{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}],
>>> Blog.objects.values('id', 'name')
[{'id': 1, 'name': 'Beatles Blog'}]
但是假设我想把名字变成另一个字典并得到这样的回报
[{'id': 1, 'blog': { 'name': 'Beatles Blog'}}]
上述是否可以通过使用 value 或类似的东西来实现?
我知道我可以做一些可行的事情
[{'id': blog.pk, 'blog': {'name': blog.name}} for blog in blogs]
提前感谢我的好奇心
【问题讨论】:
标签: python django dictionary