Set up and connect a database in a project in Django (MySQL)

- Andrés Cruz

En español

Set up and connect a database in a project in Django (MySQL)

With all the layers that our framework has, one of the main ones is that of data management, in other words it is part of our database, which can be of various types such as MySQL, MariaDB, PostgreSQL among others.

Connectors for each database engine

The main thing that you have to take into account when you want to connect to a database engine is that the connector simply varies, that is, depending on the database server you want to connect to, you simply have to vary the connector. and in the official documentation that I leave a little below you will be able to see the different types of connectors that you can use and with this the different clients.

In this entry we are going to see how we can create a connection with MySQL in Django; for that we have a couple of ways, the official or the unofficial; if we review the official documentation.

We will see that they tell us that from version 3 of Django we can use a native Driver for MySQL and MariaDB that you are free to try the official solution, but in case you get an error like the following:

*** cannot open file include 'mysql.h' ***

Testing the UNofficial version of the MySQL connector

We can try another solution that would be to install a package that will serve as a driver or connector to our MySQL, which is the following:

pip install PyMySQL

So we installed it and now we have to tell our Django project to use this driver; for that:

firstProject\firstProject\__init__py

import pymysql
pymysql.version_info = (1, 3, 13, "final", 0)
pymysql.install_as_MySQLdb()

And now we simply have to create the database that we are going to use and then establish the rest of the connections:

firstProject\firstProject\settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangofirstproyect',
        'USER': 'root',
        'PASSWORD': ''
    }
}

And with this we are ready to do more interesting operations using our MTV in Django which we will do from the next entry.

  • DATABASE_NAME We indicate the name of the database. If you are using SQLite, specify the full path including the file system to the file/database.
  • DATABASE_USER We indicate the database user that we are going to use to connect to the previous database.
  • DATABASE_PASSWORD We indicate the password of the previous user
  • DATABASE_HOST This is one of the most important elements and it is the host, if we are in a secure development environment it will be localhost or local server, but if you are in a production environment it is very likely that it will change to another or an IP.

Depending on the database that you are going to use, you will surely have to add some parameters more or less, for example in the case of SQLite, which is the one that comes by default, it is not necessary to indicate a username/password or host, since that it is simply a file.

The next thing we will see is how to create a model for our first application.

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 2d 22:21!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!