As it stands, I have a deserialiser that can import any model I throw at it and put it into my database. unfortunately it hits the database with every single model and I want to stop this.
Is there any way for me to lump lots of short saves into one big one?
Example code:
def deserialise(xml):
for x in model_list:
do work to make instance...
instance.save()
return True
is there any way to move the saving of instance out of the for loop?
You can use
transaction.commit_manually().Or
transaction.commit_on_success()which will automatically commit the saves when and if the function returns successfully.Alternatively, in django 1.4 there’s also bulk_create(), but do note the caveats listed in documentation.