django 模型models 常用字段
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
DateField.auto_now:儲存時自動設定該字段為現在日期,最後修改日期
DateField.auto_now_add:當該對象第一次被建立是自動設定該字段為現在日期,建立日期。
6、models.DateTimeField
日期時間類型 datetime
同DateField的參數
7、models.Decimal
十進制小數類型 = decimal
DecimalField.max_digits:數字中允許的最大位數
DecimalField.decimal_places:存儲的十進制位數
8、models.EmailField
一個帶有檢查 Email 合法性的 CharField
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.GenericIPAddressField
一個帶有檢查 IP位址合法性的 CharField
13、models.NullBooleanField
允許為空的布爾類型
14、models.PositiveIntegerFiel
正整數
15、models.PositiveSmallIntegerField
正smallInteger
16、models.SlugField
減号、下劃線、字母、數字
17、models.SmallIntegerField
數字
資料庫中的字段有:tinyint、smallint、int、bigint
18、models.TextField
大文本。預設對應的form标簽是textarea。
19、models.TimeField
時間 HH:MM[:ss[.uuuuuu]]
20、models.URLField
一個帶有URL合法性校驗的CharField。
21、models.BinaryField
二進制
存儲二進制資料。不能使用filter函數獲得QuerySet。
22、models.ImageField
圖檔
ImageField.height_field、ImageField.width_field:如果提供這兩個參數,則圖檔将按提供的高度和寬度規格儲存。
該字段要求 Python Imaging 庫Pillow。
會檢查上傳的對象是否是一個合法圖檔。
23、models.FileField(upload_to=None[, max_length=100, ** options])
檔案
FileField.upload_to:一個用于儲存上傳檔案的本地檔案系統路徑,該路徑由 MEDIA_ROOT 中設定
這個字段不能設定primary_key和unique選項.在資料庫中存儲類型是varchar,預設最大長度為100
24、models.FilePathField(path=None[, math=None, recursive=False, max_length=100, **options])
FilePathField.path:檔案的絕對路徑,必填
FilePathField.match:用于過濾路徑下檔案名的正規表達式,該表達式将用在檔案名上(不包括路徑)。
FilePathField.recursive:True 或 False,預設為 False,指定是否應包括所有子目錄的路徑。
例如:FilePathField(path=”/home/images”, match=”foo.*”, recursive=True)
将比對“/home/images/foo.gif”但不比對“/home/images/foo/bar.gif”
django 模型models 字段常用參數
1、null
如果是True,Django會在資料庫中将此字段的值置為NULL,預設值是False
2、blank
如果為True時django的 Admin 中添加資料時可允許空值,可以不填。如果為False則必須填。預設是False。
null純粹是與資料庫有關系的。而blank是與頁面必填項驗證有關的
3、primary_key = False
主鍵,對AutoField設定主鍵後,就會代替原來的自增 id 列
4、auto_now 和 auto_now_add
auto_now 自動建立—無論添加或修改,都是目前操作的時間
auto_now_add 自動建立—永遠是建立時的時間
5、choices
一個二維的元組被用作choices,如果這樣定義,Django會select box代替普通的文本框,
并且限定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、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
檔案上傳時的儲存上傳檔案的目錄