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