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:
- Get account row from the database
- Pass account object to a separate method
- This method performs some business rules and returns new soc code
- Update account row
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?