質問

From the official Django docs: https://docs.djangoproject.com/en/1.5/topics/db/transactions/#transaction-rollback

a.save() # Succeeds, but may be undone by transaction rollback
try:
    b.save() # Could throw exception
except IntegrityError:
    transaction.rollback()
c.save() # Succeeds, but a.save() may have been undone

Calling transaction.rollback() rolls back the entire transaction. Any uncommitted database operations will be lost. In this example, the changes made by a.save() would be lost, even though that operation raised no error itself.

Doesn't save do a commit? Then it should not be affected by the rollback or am I wrong?

(It is possible to set the parameter commit=False for the .save() function. That indicates that usually save does a commit)

役に立ちましたか?

解決

You're confusing two meanings of the word "commit".

Calling save - on a model or a form - does cause an update to the database, so in that sense it is committed to the database. However, all database operations take place inside a transaction: and those transactions can be committed or rolled back as a block. So the documentation is correct: even if you call save(commit=True), that's still part of an overall transaction that can be rolled back.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top