1. 程式人生 > >Django model欄位

Django model欄位

1、models.AutoField  自增列 = int(11)
 如果沒有的話,預設會生成一個名稱為 id 的列,如果要顯示的自定義一個自增列,必須將給列設定為主鍵 primary_key=True。
2、models.CharField  字串欄位 --必須 max_length 引數
3、models.BooleanField  布林型別=tinyint(1) 不能為空,Blank=True
4、models.ComaSeparatedIntegerField  用逗號分割的數字=varchar
  繼承CharField,所以必須 max_lenght 引數
5、models.DateField  日期型別 date


  對於引數,auto_now = True 則每次更新都會更新這個時間;auto_now_add 則只是第一次建立新增,之後的更新不再改變。
6、models.DateTimeField  日期型別 datetime
  同DateField的引數
7、models.Decimal  十進位制小數型別 = decimal
  必須指定整數位max_digits和小數位decimal_places
8、models.EmailField  字串型別(正則表示式郵箱) =varchar
  對字串進行正則表示式
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正則表示式)

13、models.GenericIPAddressField  字串型別(ip4和ip6是可選的)
  引數protocol可以是:both、ipv4、ipv6
  驗證時,會根據設定報錯
14、models.NullBooleanField  允許為空的布林型別
15、models.PositiveIntegerFiel  正Integer
16、models.PositiveSmallIntegerField  正smallInteger
17、models.SlugField  減號、下劃線、字母、數字
18、models.SmallIntegerField  數字
  資料庫中的欄位有:tinyint、smallint、int、bigint
19、models.TextField  字串=longtext
20、models.TimeField  時間 HH:MM[:ss[.uuuuuu]]
21、models.URLField  字串,地址正則表示式
22、models.BinaryField  二進位制
23、models.ImageField 圖片
24、models.FilePathField 檔案

更多欄位
1、null=True
  資料庫中欄位是否可以為空
2、blank=True
  django的 Admin 中新增資料時是否可允許空值
3、primary_key = False
  主鍵,對AutoField設定主鍵後,就會代替原來的自增 id 列
4、auto_now 和 auto_now_add
  auto_now 自動建立---無論新增或修改,都是當前操作的時間
  auto_now_add 自動建立---永遠是建立時的時間
5、choices
GENDER_CHOICE = (
(u‘M‘, u‘Male‘),
(u‘F‘, u‘Female‘),
)
gender = models.CharField(max_length=2,choices = GENDER_CHOICE)
6、max_length
7、default  預設值
8、verbose_name  Admin中欄位的顯示名稱
9、name|db_column  資料庫中的欄位名稱
10、unique=True  不允許重複
11、db_index = True  資料庫索引
12、editable=True  在Admin裡是否可編輯
13、error_messages=None  錯誤提示
14、auto_created=False  自動建立
15、help_text  在Admin中提示幫助資訊
16、validators=[]
17、upload-to