Reputation: 1
I want to have atomic transactions on all the requests in the Django app. There is the following decorator to add an atomic transaction for one request,
@transaction.atomic
def create_user(request):
....
but then, in this case, I'll have to use this decorator for all the APIs.
I tried doing this in settings.py:
DATABASES = {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'blah',
'USER': 'postgres',
'PASSWORD': 'blah',
'HOST': '0.0.0.0',
'PORT': '5432',
'ATOMIC_REQUESTS': True
}
But adding ATOMIC_REQUESTS does not seem to work in some cases. Is there any way so that I can apply atomic transactions for all APIs?
Upvotes: 0
Views: 700
Reputation: 674
Not quite sure if this the case but in the documentation, explains that you need to pass 'using' param to this decorator which is what database this operation applies to. so can you try this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'blah',
'USER': 'postgres',
'PASSWORD': 'blah',
'HOST': '0.0.0.0',
'PORT': '5432',
'ATOMIC_REQUESTS': True,
},
}
then like this:
@transaction.atomic(using='default')
def create_user(request):
I also realized that you used psycopg2 as your engine Changed in Django 1.9:
from the docs:
The django.db.backends.postgresql backend is named django.db.backends.postgresql_psycopg2 in older releases. For backwards compatibility, the old name still works in newer versions.
fyi
Upvotes: 1