Object Relationship Mapping
对象 关系 映射
(1)添加一个应用程序 python
mysql
# file : bookstore/models.py from django.db import models class Book(models.Model): title = models.CharField("书名", max_length=50, default='') price = models.DecimalField('订价', max_digits=7, decimal_places=2, default=0.0) ############ # file : setting.py INSTALLED_APPS = [ ... 'bookstore', ]
(3)迁移生成脚本文件bookstore/migrations/0001_initial.py
并进行迁移git
$ python3 manage.py makemigrations
$ python3 manage.py migrate
#终端操做 $ python3 manage.py makemigrations Migrations for 'bookstore': bookstore/migrations/0001_initial.py - Create model Book $ python3 manage.py migrate Operations to perform: Apply all migrations: admin, auth, bookstore, contenttypes, sessions Running migrations: Applying bookstore.0001_initial... OK
(4)查看数据表(是否迁移成功)web
$ mysql -u root -p mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mygoods | | mysql | | mywebdb | | onlybuyp | | performance_schema | | sys | | test_db | +--------------------+ 8 rows in set (0.00 sec) mysql> use mywebdb Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------------------+ | Tables_in_mywebdb | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | bookstore_book | <<== 新加表 | django_admin_log | | django_content_type | | django_migrations | | django_session | +----------------------------+ 11 rows in set (0.00 sec) mysql> desc bookstore_book; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | title | varchar(50) | NO | | NULL | | | price | decimal(7,2) | NO | | NULL | | +-------+--------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
(5)表bookstore_book即为模型Book类对应的数据表sql
sql> drop database 数据库名
删除数据库后从新迁移数据库模型类需继承自django.db.models.Model
数据库
(1)模型的语法规范django
from django.db import models class CLASSNAME(models.Model): NAME = models.FIELD_TYPE(FIELD_OPTIONS)
(2)班级名称编程
(3)名称bash
(4)FIELD_TYPEsession
(1)BooleanField()
(2)CharField()
(3)DateField()
(4)DateTimeField字段()
(5)DecimalField()
money=models.DecimalField( max_digits=7, decimal_places=2, default=0.0 )
(6)FloatField()
(7)EmailField()
(8)IntegerField()
(9)URLField()
(10)ImageField的()
image=models.ImageField( upload_to="static/images" )
(11)文本域()
文档参考https://docs.djangoproject.com/en/1.11/ref/models/fields/#field-types
FIELD_OPTIONS
字段选项,指定建立的列的额外的信息field_options
容许出现多个字段选项,多个选项之间使用,隔开
(1)首要的关键:primary_key=True
若是设置为True,则表示该列为主键,若是指定一个字段为主键,则此数库表不会建立ID字段
(2)空白:blank=Flase
设置为真时,字段能够为空。设置为False时,字段是必须填写的。字符型字段CharField和TextField是用空字符串来存储空值的。默认值是False。
(3)空值:null=Ture
若是设置为True,则表示该列值容许为空。日期型,时间型和数字型字段不接受空字符串。因此设置IntegerField,DateTimeField字段型字段能够为空时,须要将空(blank),空(null)均设为真(True)。
默认为False,若是此选项为false,建议加入默认(default)选项来设置默认值
(4)默认:delault=''
设置所在列的默认值,若是字段选项空=假建议添加此项
(5)db_index
若是设置为True,则表示为该列增长索引
(6)独特unique
若是设置为True,则表示该字段在数据库中的值必须是惟一(不能重复出现的)
(7)db_column
指定列的名称,若是不指定的话则采用属性名做为列名
(8)verbose_name
设置此字段在管理界面上的显示名称。
# 建立一个属性,表示用户名称,长度30个字符,必须是惟一的,不能为空,添加索引 name = models.CharField(max_length=30, unique=True,
null=False, db_index=True)
文档参见:https://docs.djangoproject.com/en/1.11/ref/models/fields/#field-options