2

我正在做一个原始查询,其中我从表中选择 id 的结果如下所示:

[(517L,), (519L,), (526L,), (537L,), (668L,), (670L,), (671L,), (672L,), (673L,)] 

我需要将这些 id 与 __in 过滤器一起使用来接收正确的对象,这是我的代码 注意我在这里编写的原始查询是一个虚拟查询,我的真实查询很复杂,这就是为什么我必须使用原始查询,

from django.db import connection, transaction
cursor = connection.cursor()
cursor.execute("SELECT id FROM table1 WHERE rule=1)
property_list= cursor.fetchall()
object_list = table1.objects.filter(pk__in=property_list)

这会导致以下错误:argument must be a string or a number, not 'list'

请指教

4

1 回答 1

3

问题是您有一个元组列表,而不是整数的“原始”列表,因为fetchall()返回整行,而不是单列。尝试:

pk__in=[p[0] for p in property_list]
于 2014-01-05T14:50:50.547 回答