04- model class

ORM framework: Object - Relationship - Mapping

The object-oriented language program automatically persist objects to a relational database. Essentially convert data from one form to another form
O Object object class represents
R represents Relations relations in a relational database table
M represents a mapping Mapping, its role is to establish contact between the O and R
by classes and objects operating a database table, without writing sql statement
in Django has built ORM framework that the data table corresponding to the class and up may only operate on a data table by classes and objects, you can also design model classes generating a table in the database

Model Class

1. Design model class

Write models.py must inherit models.Model

from django.db import models

# Create your models here.

class BookInfo ( models.Modele ): """ 图书模型类 1.图书名称,CharField 说明是一个字符串,max_length字符串的最大长度,与数据库表对应 2.出版日期,DateField是一个日期类型 数据里表里还有一个主键id,id在Django里会自动生成,不需要定义 """ btitle = models.CharField ( max_length=20 ) bpub_date = models.DateField () 
2, according to a model class-table
  • Generate migration file migration command python model class file is generated, using the manage.py  makemigrations  

  • Perform the migration file to generate tables, use the command Python manage.py  the migrate  

Django project database is used by default sqlite3, can be seen in settings, under the project will generate a db.sqlite3, how to open?

1)安装
   sudo  apt-get  install sqliteman

2)输入命令sqliteman 回车就会打开 3)File--open,选择db.sqlite3文件打开

The table generated model classes, the application name is the name of the table class name Model _ lowercase

3, according to the model-based operation deletions database table change search

Project into the shell environment
command: Python manage.py  shell  

  • Insert data into table

from booktest.models import BooKInfo # application name class name
b = BookInfo () # define a class object BookInfo
b.btitle = Properties 'Dream of the Red' b # define and assign objects
from datetime Import DATE
b.bpub_date = DATE (1990,1 1) #define publication date
b.save ()

  • In the data look-up table

BookInfo.objects.get = B2 (= ID. 1)
type (B2)
b2.btitle display Dream of the Red # Enter

  • Update the database table

b2.bpub_date=date(1990,10,10)
b2.save()

  • delete data

b2.delete()

4, class relationships and relational query model

(Association between a plurality of tables)

# 人物类
# 人物名 hname
# 性别 hgender # 年龄 hage # 关系属性 hbook 建立图书类与人物类之间的一对多的关系 的属性 class HeroInfo(models.Models): hname=models.CharField(max_length=20) # default 指定默认值,False代表男 hgender=models.BooleanFiled(defalut=False) hbook=models.ForeignKey('BookInfo') # 建立两表之间的关联 

-> File Migration generation -> The migration file list generated, hbook generated in the table, the table for the corresponding field hbook_id, fixed format: name relationship attribute the _id
-> insert data

Import BookInfo booktest.models from, HeroInfo
B = the BookInfo ()
b.btitle = 'Dream of the Red'
from datetime Import DATE
b.bpub_date = DATE (1990,1,1)
b.save ()
H = HeroInfo ()
h.hname = 'Lin'
h.hgender = True
h.hbook_id = B # id book specifies a primary key of the table
h.save ()
H2 = HeroInfo () # increasing a data
h2.hname = 'Fiction'
h2.hbook B =
H2 .save ()

Queries related books according to the book table object character information

b.heroinfo_set.all()

All contents of the query table

HeroInfo.objects.all()

Guess you like

Origin www.cnblogs.com/wysk/p/11295684.html