Django-методы model save() ленивы?
являются ли методы model save() ленивыми в django?
например, в какой строке в следующем примере кода django попадет в базу данных?
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)
1 ответов
это не имеет большого смысла, чтобы иметь ленивый сохранить, не так ли? Джанго QuerySets ленивы, модели save метод не является.
из источника django:
django/db/models/base.py, линий 424-437:
def save(self, force_insert=False, force_update=False, using=None):
    """
    Saves the current instance. Override this in a subclass if you want to
    control the saving process.
    The 'force_insert' and 'force_update' parameters can be used to insist
    that the "save" must be an SQL insert or update (or equivalent for
    non-SQL backends), respectively. Normally, they should not be set.
    """
    if force_insert and force_update:
        raise ValueError("Cannot force both insert and updating in \
            model saving.")
    self.save_base(using=using, force_insert=force_insert, 
        force_update=force_update)
save.alters_data = True
затем, save_base делает тяжелый подъем (тот же файл, строки 439-545):
...
transaction.commit_unless_managed(using=using)
...
и django/db/transaction.py, строки 167-178, вы найдете:
def commit_unless_managed(using=None):
    """
    Commits changes if the system is not in managed transaction mode.
    """
    ...
П. С. Все цифры относятся к версии Джанго  (1, 3, 0, 'alpha', 0).
