Задать вопрос

Тел: +7 965 3737 888

442

Просмотров

2

Ответов

Nested commit_on_success

<p>This is a simple modification to the standard transaction.commit_on_success decorator that is aware of existing transactions.  If a managed transaction is already active then the wrapped function is called directly, assuming the active transaction will clean up as appropriate.  Otherwise, a standard commit_on_success is performed.</p>
<p>I'm using this to wrap the save() method of models that manipulate other related models on save, e.g:</p>
@nested_commit_on_success
def save(self):
    super(MyClass,self).save()
    for m in other_models:
        self.fix_up_other_model(m)
        m.save()

Вопрос полезен? Да0/Нет0
file_5102.py(894.0б)
None

Ответы (2):

Ответdemanzano:11.08.2011
Ответ полезен? Да0/Нет0

Very useful!

just a question, this code is for a decorator (the '@' operator) but what if I would use the 'with' operator ? How should I modify this code to be run with 'with' ? Thanks anyway !

Ответdemichej:10.08.2011
Ответ полезен? Да0/Нет0

Thanks, RFK! I love it! I wonder why Django doesn't just use this decorator instead of its normal commit_on_success() what am I missing? It seems like this works exactly like commit_on_success, except it won't open a new transaction if one is already being managed?