How to make Django work with unsupported MySQL drivers such as gevent-mysql or Concurrence's MySQL driver?

╄→гoц情女王★ 提交于 2020-01-09 12:37:12

问题


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.


回答1:


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!




回答2:


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
    


来源:https://stackoverflow.com/questions/2636536/how-to-make-django-work-with-unsupported-mysql-drivers-such-as-gevent-mysql-or-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!