Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8104311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:48:21+00:00 2026-06-05T23:48:21+00:00

Django has a decorator function called @transaction.commit_manually . I am attempting to pass a

  • 0

Django has a decorator function called @transaction.commit_manually. I am attempting to pass a parameter to this decorator, (using=db). db varies depending on which database is being used, based on business rules. What is the best way to pass the current database to this decorator? I tried to use an inner function, thus:

def func(db):
    stuff = _business logic_

    @transaction.commit_manually(using=db)
    def do_transaction(stuff):
        try:
            stuff.save(using=db)
        except:
            transaction.rollback()
        else:
            transaction.commit()

    do_transaction()

However, this fails. The error I discovered with pdb is that the inner block is “Not under transaction management.” How do I overcome this problem?

Traceback from pdb:

-> success = transactional_registration()
  /usr/local/lib/python2.7/dist-packages/django/db/transaction.py(338)_commit_manually()
-> return func(*args, **kw)
> /home/syrion/dev/registration.py(59)transactional_registration()
-> transaction.rollback()
  /usr/local/lib/python2.7/dist-packages/django/db/transaction.py(210)rollback()
-> set_clean(using=using)
  /usr/local/lib/python2.7/dist-packages/django/db/transaction.py(125)set_clean()
-> raise TransactionManagementError("This code isn't under transaction management"

Edit: I fixed my own problem. The internal function solution works correctly, but I needed to be calling rollback() and commit() with a using parameter, i.e. transaction.commit(using=db), as well. I find that unintuitive, but…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T23:48:22+00:00Added an answer on June 5, 2026 at 11:48 pm

    I use this quite often in my code since I put a lot of stress on the ORM. Since I’m not a big fan of decorator syntax I use the with statement.

    def do_transaction(stuff, db):
        with transaction.commit_manually(using=db)
            try:
                stuff.save(using=db)
            except:
                transaction.rollback()
            else:
                transaction.commit(using=db)
    

    This should work. Not sure if you’re going to need (using=db) in your transaction.rollback(), though. I’m sure you can do the research on that. But you do need (using=db) in transaction.commit(using=db).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given that django-nonrel has got JOINs working, does this mean we have M2M fields
I am making a custom form object in Django which has an overrided __init__
I has a Django view which is accessed through an AJAX call. It's a
I'm working on a project, using Python/Django running on a Virtual Private Server, which
Django has a DATE_FORMAT and a DATE_TIME_FORMAT options that allow us to choose which
I have several views with @login_required decorator. And I'm going to use django.core.urlresolvers.reverse() function
Using Django 1.3 with PostgreSQL 9.0, I have a multi-step object creation function/view, where:
django has this complex ORM built in to it, but after spending much time
Django has a built in serialization functionality which allows you to serialize any query
I have an existing Django project that has several models using concrete inheritance of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.