How to config Django using pymysql as driver?
Asked Answered
N

3

22

I'm new to Django. It wasted me whole afternoon to config the MySQL engine. I am very confused about the database engine and the database driver. Is the engine also the driver? All the tutorial said that the ENGINE should be 'django.db.backends.mysql', but how the ENGINE decide which driver is used to connect MySQL?

Every time it says 'django.db.backends.mysql', sadly I can't install MySQLDb and mysqlclient, but PyMysql and the official mysql connector 2.1.3 has been installed. How could I set the driver to PyMysql or mysql connector?

Many thanks!

  • OS: OS X Al Capitan
  • Python: 3.5
  • Django: 1.9

This question is not yet solved: Is the ENGINE also the DRIVER?

Necrophilism answered 13/1, 2016 at 21:50 Comment(0)
A
75

You can import pymsql so it presents as MySQLdb. You'll need to do this before any django code is run, so put this in your manage.py file

import pymysql
pymysql.install_as_MySQLdb()
Amentia answered 13/1, 2016 at 22:18 Comment(1)
And for the DATABASE entry: 'ENGINE': 'django.db.backends.mysql',Unbar
I
1

The short answer is no they are not the same.

The engine, in a Django context, is in reference to RDBMS technology. The driver is the library developed to facilitate communication to that actual technology when up and running. Letting Django know what engine to use tells it how to translate the ORM functions from a backend perspective. The developer doesn't see a change in ORM code but Django will know how to convert those actions to a language the technology understands. The driver then takes those actions (e.g. selects, updates, deletes) and sends them over to a running instance to facilitate the action.

Isle answered 7/11, 2018 at 17:51 Comment(0)
S
0

Update. I'm running Django 4.2.11 under apache using mod_wsgi.

In order to get this to work, modify the wsgi.py script (or whatever you called it) to include the code snippet from user764357. By the time the settings.py is loaded, DJango has already got things setup or failed.

Stumpage answered 7/5 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.