Django - Является образцовое сохранение () ленивыми методами?

Является образцовое сохранение () методами, ленивыми в 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)
7
задан Jonathan 9 July 2010 в 13:27
поделиться

1 ответ

Нет никакого смысла делать ленивые сохранения, не так ли? Django 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.
    """
    ...

PS Все номера строк относятся к версии django (1, 3, 0, 'alpha', 0) .

7
ответ дан 7 December 2019 в 07:39
поделиться
Другие вопросы по тегам:

Похожие вопросы: