現(xiàn)在mysql命令行創(chuàng)建數(shù)據(jù)庫:
mysql>create database hahadefaultcharset=utf8;
然后在django下的app的models.py建立表氧敢,不用去mysql建
classCharacter(models.Model):
? ? name=models.CharField(max_length=100)
? ? def__unicode__(self):
? ? ? ? return self.name
更新數(shù)據(jù)庫
python manage.py makemigrations?
python manage.pymigrate
完成冰啃。
可用的類型:
1遗嗽、models.AutoField 自增列=int(11)
如果沒有的話絮缅,默認(rèn)會生成一個名稱為id的列堡妒,如果要顯示的自定義一個自增列晶框,必須將給列設(shè)置為主鍵 primary_key=True蜕径。
2、models.CharField 字符串字段
必須 max_length 參數(shù)
3司草、models.BooleanField 布爾類型=tinyint(1)
不能為空艰垂,Blank=True
4、models.ComaSeparatedIntegerField 用逗號分割的數(shù)字=varchar
繼承CharField埋虹,所以必須 max_lenght 參數(shù)
5猜憎、models.DateField 日期類型 date
對于參數(shù),auto_now=True則每次更新都會更新這個時間搔课;auto_now_add 則只是第一次創(chuàng)建添加胰柑,之后的更新不再改變。
6爬泥、models.DateTimeField 日期類型 datetime
同DateField的參數(shù)
7柬讨、models.Decimal 十進(jìn)制小數(shù)類型=decimal
必須指定整數(shù)位max_digits和小數(shù)位decimal_places
8、models.EmailField 字符串類型(正則表達(dá)式郵箱)=varchar
對字符串進(jìn)行正則表達(dá)式
9袍啡、models.FloatField 浮點類型=double
10踩官、models.IntegerField 整形
11、models.BigIntegerField 長整形
integer_field_ranges={
'SmallIntegerField':(-32768,32767),
'IntegerField':(-2147483648,2147483647),
'BigIntegerField':(-9223372036854775808,9223372036854775807),
'PositiveSmallIntegerField':(0,32767),
'PositiveIntegerField':(0,2147483647),
}
12境输、models.IPAddressField 字符串類型(ip4正則表達(dá)式)
13蔗牡、models.GenericIPAddressField 字符串類型(ip4和ip6是可選的)
參數(shù)protocol可以是:both、ipv4嗅剖、ipv6
驗證時蛋逾,會根據(jù)設(shè)置報錯
14、models.NullBooleanField 允許為空的布爾類型
15窗悯、models.PositiveIntegerFiel 正Integer
16、models.PositiveSmallIntegerField 正smallInteger
17偷拔、models.SlugField 減號蒋院、下劃線亏钩、字母、數(shù)字
18欺旧、models.SmallIntegerField 數(shù)字
數(shù)據(jù)庫中的字段有:tinyint姑丑、smallint、int辞友、bigint
19栅哀、models.TextField 字符串=longtext
20、models.TimeField 時間 HH:MM[:ss[.uuuuuu]]
21称龙、models.URLField 字符串留拾,地址正則表達(dá)式
22、models.BinaryField 二進(jìn)制
23鲫尊、models.ImageField圖片
24痴柔、models.FilePathField文件