最简单(但不是最安全)的方法是使用raw sql query。
就像是:
for prd,count in x.iteritems():
from django.db import connection, transaction
cursor = connection.cursor()
query = """
UPDATE {table}
set {column} = {value}
where {condition} = {condition_value}""".format(
table=AggregatedResult._meta.db_table,
condition='product',
condition_value=prd,
column='linked_epp_count',
value=count
)
cursor.execute(query)
transaction.commit_unless_managed()
警告:未经测试,极易受到 sql 注入的攻击。使用风险自负
替代(更安全)的方法是首先将内容加载x
到临时表中,而不是只发出一个原始查询来更新。假设 x 的临时表是temp_prod
:
update aggregated_result ar
set linked_epp_count=tp.count
from temp_prod tp
where ar.product = tp.product
你如何将数据从x
临时表上传到我不是很精通,所以留给你。:)