Django/postgres事务隔离级别设置被忽略

Django/postgres事务隔离级别设置被忽略,django,postgresql,Django,Postgresql,我有一个问题设置事务隔离级别。我想要最严格的序列化,而默认值是readcommitted 我正在使用: Django==1.10.6 psycopg2==2.5.1 在heroku上运行 根据文件: 我有以下设置.py: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

我有一个问题设置事务隔离级别。我想要最严格的序列化,而默认值是readcommitted

我正在使用:

Django==1.10.6
psycopg2==2.5.1
在heroku上运行

根据文件:

我有以下设置.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'OPTIONS': {
        'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
    }
}

if on_heroku:
    DATABASES['default'] = dj_database_url.config()
以下是查看代码:

@require_http_methods(["POST"])
@transaction.atomic
@login_required()
def api_test_add_one(request):
    cursor = connection.cursor()

    cursor.execute('SHOW default_transaction_isolation')

    logger.info("aa: " + str(cursor.fetchone()))

    return HttpResponse("{}", content_type="application/json")
输出为:

aa: (u'read committed',)
我在访问同一端点的同时运行了不同的测试,以DB为单位递增一个整数,并确认事务未被隔离。

这些选项属于数据库的设置,例如

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgres',
        ...
        'OPTIONS': {
            'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
        },
    },
}
在您的情况下,您将替换
数据库['default']
,因此您在那里设置的
选项将丢失:

if on_heroku:
    DATABASES['default'] = dj_database_url.config()
相反,您可以在设置
数据库['default']
后设置
选项

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
}

if on_heroku:
    DATABASES['default'] = dj_database_url.config()
    DATABASES['default']['OPTIONS'] = {
        'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE,
    }

就这样,我希望我早点问!