3

Ok so I am very familiar with django transactions inside of views and pre-1.5 you just had to add the decorator @transaction.commit_on_success. This worked on views AND on standalone scripts. When we updated to 1.6 I just commented all of these out if they were not in a view. I found that if you had a standalone script using @transaction.atomic it all blew up (nothing actually saves to the database). So I now have a pretty big need to get this working again after just disabling it. All the docs simply refer to transactions in a view and give no instruction on what to do if you are using django outside a view!

So far I have tried:

  • Turning off autocommit (I'm using postgres)
  • Creating savepoints
  • Using atomic

None of these work. Is the only solution to run raw SQL to begin and commit a transaction?

Any good examples in stack overflow also exclusively deal with views or did not have any answers.

Community
  • 1
  • 1
hazmat
  • 640
  • 5
  • 12
  • Django's default behaviour in 1.6+ is to run in autocommit mode, so I wouldn't recommend turning it off. The `atomic()` decorator does work in scripts as well as views. Your question isn't very specific, so I don't have any ideas why it isn't working in this case. – Alasdair Sep 15 '15 at 08:52
  • Also, note that `commit_on_success` is deprecated, but still available in Django 1.6 and 1.7. You don't have to comment it out or remove it until you get your code working with the new transaction APIs. – Alasdair Sep 15 '15 at 08:57
  • Yes, I know. It worked until we upgraded to 1.8. We have some scripts that run some processing and we noticed that anything inside the atomic decorator was not being saved. We simply ripped everything out, but that was not a long-term solution. – hazmat Sep 17 '15 at 21:12
  • I was able to get it to work by getting the cursor and manually calling begin and commit. – hazmat Sep 17 '15 at 21:13
  • 1
    Hey, I'm facing the same issue now, realizing that transaction.atomic only works for view functions. Can you please elaborate on how you solved it? (if you remember of course) – user2880391 Mar 21 '18 at 11:17
  • `transaction.atomic` works outside of view functions, only not as a decorator. Use it as a context manager instead. – GDorn Jun 09 '20 at 00:36

0 Answers0