Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use MySQL on Django(dev 1.6.x) with Python3.x?

I use Django dev(1.6.x) from git repo and I want to use MySQL , But on the settings.py file can not setup MySQL because MySQL is not supported in python3 and Django, So I used pymysql package on python3.x without any problem but in Django can not setup that on settings.py too.

Can I use mysql(or pymysql or ?) on django with python3 ?

like image 706
alireza Avatar asked Nov 10 '12 07:11

alireza


People also ask

Is MySQL compatible with Django?

Django supports MySQL 5.7 and higher.

What should be the database engine for MySQL database in Django?

In theory, Django empowers us to switch between DB Engines without updating the SQL code. The default SQLite database usually covers all requirements for small or demo projects but for production use, a more powerful database engine like MySql or PostgreSQL is recommended.

How to configure Django with MySQL?

Create any database which you want to use on your Django Project. Install the following packages in the virtualenv (if you're using django on virtualenv, which is more preferred): That's it!! you have configured Django with MySQL in a very easy way.

What is Django and why should you learn it?

For newcomers, Django is a leading Python web framework built by experts using a bateries-included concept. Being such a mature framework, Django provides an easy way to switch from the default SQLite database to other database engines like MySql, PostgreSQL, or Oracle.

How do I create a database in Django?

You can configure your web server as you want but by default web server is at http://localhost:80 and database at port 3306, and PhpMyadmin at http://localhost/phpmyadmin/ From here you can see your databases and access them using very friendly GUI. Create any database which you want to use on your Django Project.

What is batteries included framework in Django?

It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc. How to integrate Mysql database with Django ?


2 Answers

I also struggled with making MySQL work with Django 1.6 and Python 3.3; the only thing that worked was to switch to PyMySQL. See my post on that here

Adding the answer below

My environment: OSX 10.9, Python 3.3.3, Django 1.6.1, PyMySQL 0.6.1, MySQL Server 5.5 on Windows

How to make it work:

  1. Install PyMySQL version 0.6.1 (https://github.com/PyMySQL/PyMySQL/): you can install it either by using pip, i.e. : pip install PyMySQL or by manually downloading the package; there is a good documentation on their website on how to do that.

  2. Open your Django App __init__.py and paste the following lines:

    import pymysql pymysql.install_as_MySQLdb()  
  3. Now, open settings.py and make sure your DATABASE property looks like this:

    DATABASES = {    'default': {        'ENGINE': 'django.db.backends.mysql',        'NAME': 'mydb',        'USER': 'dbuser',        'PASSWORD': 'dbpassword',        'HOST': 'dbhost',        'PORT': '3306'     } } 
  4. That's it, you should be able to execute python manage.py syncdb to init your MySQL DB; see the sample output below:

    Creating tables ... Creating table django_admin_log Creating table auth_permission Creating table auth_group_permissions ... ... Creating table socialaccount_socialtoken  You just installed Django's auth system, which means you don't have any superusers defined. ... 
like image 167
morgan_il Avatar answered Sep 18 '22 14:09

morgan_il


pymysql for python 3 is not a Django DB backend, however there is evidence that some work has been made porting the MySQL Backend to be Python 3 compatible at Django Python 3 MySQL backend changes.

Other pages show that the MySQL backend given with Django 1.5 works with Python 3 : Django MySQL Works on Python 3.2.2

The default MySQL wrapper available on Python 2.x (mysql-python) does however not support Python 3. I suspect the package at MySQL Python 3 to be compatible, you might give it a try.

Also make sure you're running Django with Python 3.2 or 3.3 or above. Python 3.0 is not supported.

If it still does not work after these checks, please post your DATABASES settings in case something was wrong in it.

Also, I don't find anything above Django 1.5 in the Django repos, are you sure it's 1.6.x ?

like image 23
Steve K Avatar answered Sep 22 '22 14:09

Steve K