How to distinctly bulk update all objects of a django model without iterating over them in python? -


basically can achieve same result without doing this:

from my_app import models prd,count in x.iteritems():     models.aggregatedresult.objects.filter(product=prd).update(linked_epp_count=count) 

? evident, x dictionary containing keys same aggregatedresult's product field , 'value' count wish update. taking more 2 - 3 minutes run on test table having < 15k rows , size of table ~ 200k , expected grow upto million. so, need help.

easiest (but not safest) way use raw sql query.

like:

for prd,count in x.iteritems():     django.db import connection, transaction     cursor = connection.cursor()      query = """         update {table}          set {column} = {value}          {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() 

warning: not tested , extremely vulnerable sql-injections. use @ own risk

alternative (much safer) approach first load contents of x temporary table, issue 1 raw query update. assuming temp table x temp_prod:

update aggregated_result ar set linked_epp_count=tp.count temp_prod tp ar.product = tp.product 

how upload data x temp table i'm not proficient with, it's left you. :)


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -