Model design

  • Current project development is data-driven, and analyze the data needed to be stored in the project, then design the data table and structure, and then use the designed page to complete the CRUD of data
  • Djangos built-in ORM framework does not require direct database programming. Instead, it defines model classes and uses model classes and objects to cruD data tables
  • The steps for database development using Django are as follows:
  1. Define model classes in models.py
  2. The migration

3. Data CRUD through classes and objects

1. Define model classes

  • The Model class is defined in the models.py file and inherits from the models.model class
  • Note: Primary key columns do not need to be defined, they are added automatically in the build and grow automatically
  • Book table structure design:
  • Book table structure design:
  1. The table name: BookInfo
  2. Book name: btitle
  3. Book release date: bpub_date
  • Hero table structure design:
  1. The table name: HeroInfo
  2. Hero name: hname
  3. Hero gender: Hgender
  4. Hero profile: hcontent
  5. Title: HBook
  • The book-hero relationship is one-to-many
  • By design, the model class is defined in models.py as follows:

Migration of 2.

  • The default is sqlite3 database
  • Directory structure before migration

  • The migration is done in two steps
  1. Generate migration files: Generate statements to create tables from model classes
  2. Perform the migration: Create tables in the database based on the statements generated in the first step
  • Generate the migration file command
python manage.py makemigrations
Copy the code

  • Run the following migration commands:

  • Directory structure after migration:

3. Data operation

  • Enter the shell of the project and perform simple API operations
  • To exit the shell, press CTRL + D or enter quit ()
python manage.py shell
Copy the code

  • Start by introducing the classes in bookTest/Models
from booktest.models import BookInfo,HeroInfo
Copy the code
  • View all book information
BookInfo.objects.all()
Copy the code
  • The empty list is returned because there is no data currently available

  • Creating a New book Object
b=BookInfo()
b.btitle="Legend of the Condor Heroes"
from datetime import date
b.bpub_date=date(1990.1.10)
b.save()
Copy the code

  • Query all books again
b=BookInfo.objects.get(pk=1)
b
b.id
b.btitle
b.bpub_date
Copy the code

  • Modifying book Information
B.b pub_date = date (2017,1,1) b.s ave () b.b pub_dateCopy the code

  • Deleting book Information
b.bpub_date=date(2017.1.1)
b.save()
b.bpub_date
Copy the code

Object associated operations

  • You can ctud HeroInfo in the same way as above
  • Create a BookInfo object
B = BookInfo () b.b title = 'ABC' b.b pub_date = date (2017,1,1) b.s ave ()Copy the code
  • Create a HeroInfo object
h=HeroInfo()
h.htitle='a1'
h.hgender=True
h.hcontent='he is a boy'
h.hBook=b
h.save()
Copy the code
  • Books and heroes have a one-to-many relationship. Django provides related operations
  • Get associative collection: Returns all heros of the current book object
b.heroinfo_set.all()
Copy the code