绑定列表参数与Django 1.3 ORM原始查询

时间:2014-03-12 14:24:12

标签: python django binding orm

如何在Django 1.3 ORM原始查询中绑定列表? 我想要的例子:

customers = [1, 2, 3]
# ....
customers_filter = "and products.customer_id in (%(customers)s)"
sql_params['customers'] = customers # <<< how to bind list?
# ....
cursor.execute(sql, sql_params)

1 个答案:

答案 0 :(得分:0)

示例,我如何解决该问题

total = {}

if customers:
    sql_params.update({'customer_%s' % index: customer for index, customer in enumerate(customers)})
    customers_filter = 'and t.customer_id in (%s)' % ', '.join(
        '%%(customer_%s)s' % index for index, customer in enumerate(customers))