Django Starter Design Model

define model class

  • There is a data table, there is a model class corresponding to it
  • Open the models.py file and define the model class
  • Import packages from django.db import models
  • Model classes inherit from the models.Model class
  • Description: There is no need to define the primary key column, it will be added automatically when it is generated, and the value will grow automatically
  • When the object is output, the str method of the object is called

E.g:

  • This example completes the maintenance of the "Book-Hero" information, and needs to store two kinds of data: books, heroes
  • Book table structure design:
    • 表名:BookInfo
    • Book name: btitle
    • Book published: bpub_date
  • Hero table structure design:
    • Table name: HeroInfo
    • Hero name: hname
    • Hero gender: hgender
    • Hero Profile: hcontent
    • Belongs to the book: hbook
  • The book-hero relationship is one-to-many

Relevant code:

from django.db import models

class BookInfo(models.Model):
    btitle = models.CharField(max_length=20)
    bpub_date = models.DateTimeField()
    def _ _str_ _(self):
        return "%d" % self.pk

class HeroInfo(models.Model):
    hname = models.CharField(max_length=20)
    hgender = models.BooleanField()
    hcontent = models.CharField(max_length=100)
    hBook = models.ForeignKey('BookInfo')
    def _ _str_ _(self):
        return "%d" % self.pk


Then generate the data table:

  • Activate the model: Edit the settings.py file and add the booktest application to the installed_apps of settings.py

        



  • Generate migration files: generate sql statements based on model classes
python manage.py makemigrations
  • Migration files are generated into the application's migrations directory

        

  • Execute migration: execute sql statement to generate data table
python manage.py migrate

test data manipulation

  • Enter the python shell for simple model API exercises
python manage.py shell
  • After entering the shell, the prompt is as follows:

        


  • Import the required packages:
from booktest.models import BookInfo,HeroInfo
from django.utils import timezone
from datetime import *
  • Query all book information:
BookInfo.objects.all()
  • New book information:
b = BookInfo()
b.btitle="射雕英雄传"
b.bpub_date=datetime(year=1990,month=1,day=10)
b.save()
  • Find book information:
b=BookInfo.objects.get(pk=1)
  • Output book information:
b
b.id
b.btitle
  • Edit book information:
b.btitle=u"天龙八部"
b.save()
  • Delete book information:
b.delete()

Operations on Associated Objects

  • For HeroInfo, it can be done as above
  • Add, pay attention to adding associated objects
h=HeroInfo()
h.htitle=u'郭靖'
h.hgender=True
h.hcontent=u'降龙十八掌'
h.hBook=b
h.save()
  • Get the associated collection: return all heroes of the current book object
b.heroinfo_set.all()
  • For a HeroInfo to exist, there must be a BookInfo object that provides the data to create the association:
h=b.heroinfo_set.create(htitle=u'黄蓉',hgender=False,hcontent=u'打狗棍法')
h


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324379707&siteId=291194637