I am new to the python and Django world. I don't know if there is a better way of doing the following:
#retrieve account row, correct soc code and save record.
for i in xrange(0,len(accIdList)):
acc = account.objects.filter(id=accIdList[i])[0]
acc.soc = getAccSocCode(acc)
acc.save()
def getAccSocCode(acc):
#apply business rules on acc
# and return new soc code
return socCode
This is what my code is doing now:
Here, I am updating each account row separately, is there a better way to this like below:
#retrieve account row, correct soc code and save record.
accList = []
for i in xrange(0,len(accIdList)):
acc = account.objects.filter(id=accIdList[i])[0]
acc.soc = getAccSocCode(acc)
accList.append(acc)
accList.save()
I tried the above code but it throws an error saying AttributeError: 'list' object has no attribute 'save'
In short, how to perform bulk update in Django?
Use F objects - see: How to 'bulk update' with Django? .
Even if you are saving one by one you should change your code to something like:
for id in accIdList:
acc = account.objects.get(id)
acc.soc = getAccSocCode(acc)
acc.save()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.