【发布时间】:2013-01-10 18:10:21
【问题描述】:
我需要执行原始查询:
cursor.execute("select id, name from people")
results = cursor.fetchall()
如何转换它以便在 Django 模板中使用它:
{% for person in results %}
{{person.name}}
{% endfor %}
通常,我会使用模型:
results = people.objects.raw("select id, name from people")
无论我在查询中使用多少其他模型/表,它都有效。
但是,该方法要求我包含人员模型的主 ID。这次我不能这样做,因为 sql 实际上是一个 group by 查询,并且不能包含 id。
我绝对想使用原始 sql,而不是其他类似“group by”的方式。
【问题讨论】:
-
您想要执行的实际查询是什么?也许可以通过 django ORM 来做到这一点。
标签: python django django-templates