我正在做一个原始查询,我从表中选择 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'
请指教
请您参考如下方法:
问题是您有一个元组列表,而不是“原始”整数列表,因为 fetchall() 返回整行,而不是单列。尝试:
pk__in=[p[0] for p in property_list]
