Web Project Using Django: Connect With PostgreSQL

Django could interact with mulitple databases such as MySQL, SQLite(default), PostgreSQL… Take PostgreSQL as an example, I will show you how django connect with database.

Psycopg2

Psycopg is a PostgreSQL adapter for the Python programming language. It is a wrapper for the libpq, the official PostgreSQL client library. Psycopg2 is a adapter written in C, in the first article I have shown you how to install it:

1
pip install psycopg2

After installation, you just import this package in your python file, but in django, it has no need to do that.

Django Database Connection

Open settings.py, it’s a normal Python module with module-level variables representing Django settings. Find field DATABASE, configuration in django about postgreSQL is below:

1
2
3
4
5
6
7
8
9
10
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'testdb',
'USER': 'postgres',
'PASSWORD': 'applysquare',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}

NAME is your database name, ENGINE is your database type, like ‘django.db.backends.sqlite3’, ‘django.db.backends.postgresql’, ‘django.db.backends.mysql’, or ‘django.db.backends.oracle’, others can see this.

When you change the file settings.py, remember run the command:

1
python manage.py migrate

The migrate command creates any necessary database tables according to the database settings in your mysite/settings.py file and the database migrations shipped with the app (we’ll cover those later).

Creating models

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.

create models match to database

In my case, I need to read data from database, so I have to create models that match to database’s table structure and I do not need to create my own models.

1
python manage.py makemigrations query

Run the command above and it will create models.py automatically, and you could see creation information in the terminal at the same time.

create your own models

Take official tutorial as an example:

In our simple poll app, we’ll create two models: Question and Choice. A Question has a question and a publication date. A Choice has two fields: the text of the choice and a vote tally. Each Choice is associated with a Question.
These concepts are represented by simple Python classes. Edit the polls/models.py file so it looks like this:

file: polls/models.py

1
2
3
4
5
6
7
8
9
10
11
12
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

The code is straightforward. Each model is represented by a class that subclasses django.db.models.Model. Each model has a number of class variables, each of which represents a database field in the model.

Each field is represented by an instance of a Field class – e.g., CharField for character fields and DateTimeField for datetimes. This tells Django what type of data each field holds.

The name of each Field instance (e.g. question_text or pub_date) is the field’s name, in machine-friendly format. You’ll use this value in your Python code, and your database will use it as the column name.

Then run command:

1
python manage.py migrate

The migrate command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your mysite/settings.py file and the database migrations shipped with the app.

Reference

Django Offical Documentation ver 1.11