Django version 1.9 build data table process:
Environment: windows, pycharm version 4.0, django version 1.9
1. Create a new django project and build an app
file-new project --django --write project name --more setting --write app name --create
2. Set setting.py
Database settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'password', # Not used with sqlite3.
'HOST': '10.xxx.xxx.xxx', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
}
}
add app
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'books',
)
3. model.py settings in the app
from django.db import models
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
class Author(models.Model):
salutation = models.CharField(max_length=10)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
headshot = models.ImageField(upload_to='/tmp')
class Book (models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
4. Run the command in the project directory
Create a new sql file:
python manage.py makemigrations
$ python manage.py makemigrations
Migrations for 'books':
0001_initial.py:
- Create model Author
- Create model Book
- Create model Publisher
- Add field publisher to book
update to database
Running: python manage.py migrate
$ python manage.py migrate
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, contenttypes, books, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying books.0001_initial... OK
5. Complete the establishment of the corresponding table in the database.
mysql> show tables;
+----------------------------+
| Tables_in_test |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| books_author |
| books_book |
| books_book_authors |
| books_publisher |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
| django_site |
+----------------------------+
15 rows in set (0.00 sec)
The exception to the "one class per database table" rule is a many-to-many relationship. In our paradigm model, Book has a many-to-many field called authors . This field indicates that a book has one or more authors, but the Book database table does not have an authors field. Instead, Django creates an additional table (a many-to-many join table) to handle the mapping between books and authors.
Correspondingly generate a books_book_authors correspondence table
We don't explicitly define any primary keys for these models. Django automatically creates a primary key called id for each model unless you specify it . Every Django model must have a single-column primary key.