Are model save() methods lazy in django?
For instance, at what line in the following code sample will django hit the database?
my_model = MyModel()
my_model.name = 'Jeff Atwood'
my_model.save()
# Some code that is independent of my_model...
model_id = model_instance.id
print (model_id)
It does not make much sense to have a lazy save, does it? Django’s
QuerySetsare lazy, the model’ssavemethod is not.From the django source:
django/db/models/base.py, lines 424–437:Then,
save_basedoes the heavy lifting (same file, lines 439–545):And in
django/db/transaction.py, lines 167–178, you’ll find:P.S. All line numbers apply to django version
(1, 3, 0, 'alpha', 0).