在Django项目中设置Sqlite PRAGMA选项的位置 [英] Place to set Sqlite PRAGMA option in Django project

查看:191
本文介绍了在Django项目中设置Sqlite PRAGMA选项的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此测试,设置PRAGMA同步= OFF Sqlite可以显着提高Sqlite写入性能。



我很清楚缺点,但仍然希望尝试这一点。 b
$ b

在设置此PRAGMA选项的Django项目中,最佳位置是什么?



我不能从settings.py - 至少不是文章建议的方式 - 因为django.db import连接中的会导致递归导入错误。

解决方案

将此代码添加到您安装的应用程序之一的 __ init __。py

  from django.db.backends.signals import connection_created 
def activate_foreign_keys(sender,connection,** kwargs):
使用sqlite启用完整性约束。
如果connection.vendor =='sqlite':
cursor = connection.cursor()
cursor.execute('PRAGMA foreign_keys = ON;')

connection_created.connect(activate_foreign_keys)
pre>

According to this test, setting PRAGMA synchronous=OFF Sqlite can dramatically improve Sqlite write performance.

I am well aware of the drawbacks, but would still like to try this out.

What would be the best location within a Django project to set this PRAGMA option?

I cannot do it from settings.py - at least not the way the article suggests - because from django.db import connection would cause a recursive import error.

解决方案

Add this code in the __init__.py file of one of your installed app:

from django.db.backends.signals import connection_created
def activate_foreign_keys(sender, connection, **kwargs):
    """Enable integrity constraint with sqlite."""
    if connection.vendor == 'sqlite':
        cursor = connection.cursor()
        cursor.execute('PRAGMA foreign_keys = ON;')

connection_created.connect(activate_foreign_keys)

这篇关于在Django项目中设置Sqlite PRAGMA选项的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆