如何使 Django 与不受支持的 MySQL 驱动程序(例如 gevent-mysql 或 Concurrence 的 MySQL 驱动程序)一起工作?

发布于 2024-08-29 00:44:44 字数 212 浏览 3 评论 0原文

我有兴趣在 Concurrence 或 gevent 等异步框架上运行 Django。这两个框架都带有自己的异步 MySQL 驱动程序。

问题是 Django 仅官方支持 MySQLdb。我需要做什么才能使 Django 与 gevent 或 Concurrence 附带的 MySQL 驱动程序一起工作?

有我可以遵循的分步指南吗?这是一项重大事业吗?

谢谢。

I'm interested in running Django on an async framework like Concurrence or gevent. Both frameworks come with its own async MySQL driver.

Problem is Django only officially supports MySQLdb. What do I need to do to make Django work with the MySQL drivers that come with gevent or Concurrence?

Is there a step-by-step guide somewhere that I can follow? Is this a major undertaking?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我鬧 2024-09-05 00:44:44

为 @traviscline 建议使用 pymysql 欢呼三声。他的建议基于 mozilla 的这篇文章 。所需要的只是对manage.py文件进行简单的修补

#!/usr/bin/env python
+try:
+    import pymysql
+    pymysql.install_as_MySQLdb()
+except ImportError:
+    pass 

更改导入你的设置文件和monkeypatch(),因为pymysql是一个纯Python驱动程序。

travis 提到,他通过更改导入并运行 pymysql、mysqldb 和 myconnpy 的单元测试来测试兼容性。

请注意,已经有需要注意的更精细细节的示例 - 但总的来说,这是一个优雅的、可维护的解决方案。当我在生产环境中运行时我会更新!

three cheers for @traviscline's suggestion to go with pymysql. his suggestion was based on this post from mozilla. all it takes is a simple patch to your manage.py file

#!/usr/bin/env python
+try:
+    import pymysql
+    pymysql.install_as_MySQLdb()
+except ImportError:
+    pass 

changing the import in your settings file, and monkeypatch() since pymysql is a pure python driver.

travis mentioned that he tests for compatability by changing the imports and running the unittests for pymysql, mysqldb, and myconnpy.

note that there are already examples of finer details to watch out for - but overall this is an elegant, maintainable solution. i will update when i get this running in production!

陈年往事 2024-09-05 00:44:44

我成功地让 pymysql 与 Django 一起工作,执行以下操作:

  1. 注释掉 base.py 文件开头的 try- except 块,其中导入了 MySQLdb。
  2. 将以下四行添加到base.py

    <前><代码>尝试:
    导入 pymysql 作为数据库
    除了导入错误:
    经过

  3. 如链接中所述 egbutter 已发布,转到 base.py 文件并在文件的相关部分将 MySQLdb 替换为 pymysql,即不必打扰更改错误消息(您可以,但这取决于您)。

  4. 保存base.py,并从apt位置运行以下命令以查看服务器启动。

    python manage.py runserver
    

I was successful in getting pymysql to work with Django doing the following :

  1. Comment out the try-except block at the beginning of the base.py file, where MySQLdb is imported.
  2. Add the following four lines to base.py

    try:
        import pymysql as Database
    except ImportError:
        pass
    
  3. As mentioned in the link that egbutter posted, go to the base.py file and find-replace MySQLdb with pymysql at relevant portions of the file, i.e. don't bother changing the error messages (you could, but that's up to you).

  4. Save base.py, and run the following command from the apt location to see the server start up.

    python manage.py runserver
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文