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:
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:
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:
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.
|
|
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
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:
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.