1. First define model classes in related subapplications



The code is as follows:

from django.db import models

# Create your models here.
Prepare the model class for the book list information
class BookInfo(models.Model) :
    # create field, field type...
    name = models.CharField(max_length=10)

Prepare the model class for the character list information
class PeopleInfo(models.Model) :
    name = models.CharField(max_length=10)
    gender = models.BooleanField()
    # foreign key constraint: which book the character belongs to
    book = models.ForeignKey(BookInfo)
Copy the code

2. Model migration (table building)

Migration is accomplished in two steps:

Generate migration files: Generate statements to create tables from model classes

python manage.py makemigrations
Copy the code

Perform the migration: Create tables in the database based on the statements generated in the first step

python manage.py migrate
Copy the code

If a file 0001 is displayed after the migration, the database migration is complete and the database has corresponding tables