1. 程式人生 > >Django model資料型別

Django model資料型別

model資料型別與資料庫型別類似,只不過寫法不一樣,這裡介紹一些我在專案中用到的基本型別:
1、AutoField
一個自增的IntegerField,一般不直接使用,Django會自動給每張表新增一個自增的primary key。

2、BigIntegerField
64位整數, -9223372036854775808 到 9223372036854775807。預設的顯示widget 是 TextInput.

3、BinaryField ( Django 1.6 版本新增 )
儲存二進位制資料。不能使用 filter 函式獲得 QuerySet

4、BooleanField
True/False,預設的widget 是 CheckboxInput。
如果需要置空,則必須用 NullBooleanField 代替。
Django 1.6 修改:BooleanField 的預設值 由 False 改為 None,在 default 屬性未設定的情況下。

5、CharField
儲存字串。必須有 max_length 引數指定長度。預設的form widget 是 TextInput
如果字串巨長,推薦使用 TextField。

6、DateField
日期,與python裡的datetime.date 例項同。有以下幾個可選的選項,均為bool型別:
DateField.auto_now: 每次執行 save 操作的時候自動記錄當前時間,常作為最近一次修改的時間 使用。注意:總是在執行save 操作的時候執行,無法覆蓋。
DateField.auto_now_add: 第一次建立的時候添加當前時間。常作為 建立時間 使用。注意:每次create 都會呼叫。
預設的form widget 是 TextInput。
注意:設定auto_now 或者 auto_now_add 為 True 會導致當前自動擁有 editable=False 和 blank = True 設定。

7、DateTimeField
日期+時間。與python裡的 datetime.datetime 例項同。常用附加選項和DateField一樣。
預設 form widget 是一個 TextInput

8、DecimalField
設定了精度的十進位制數字。

9、EmailField
在 CharField 基礎上附加了 郵件地址合法性驗證。不需要強制設定 max_length
注意:當前預設設定 max_length 是 75,雖然已經不符合標準,但為了向前相容,未修改。

10、FileField
檔案上傳。不支援 primary_key 和 unique 選項。否則會報 TypeError 異常。必須設定FileField.upload_to 選項,這個是 本地檔案系統路徑,附加在 MEDIA_ROOT 設定的後邊,也就是 MEDIA_ROOT 下的子目錄相對路徑。
預設的form widget 是 FileInput。
使用 FileField 和 ImageField 需要以下步驟:
(1)修改 settting.py,設定 MEDIA_ROOT(使用絕對路徑),指定使用者上傳的檔案儲存在哪裡。設定 MEDIA_URL,作為 web地址 字首,要保證 MEDIA_ROOT 目錄對執行 Django 的使用者是可寫的;
(2)在 model 中增加 FileField 或 ImageField,並指定 upload_to 選項指定存在 MEDIA_ROOT 的哪個子目錄裡;
(3)存在資料庫裡的是什麼東西呢?是 File 或 Image相對於 MEDIA_ROOT 的相對路徑,你可以在 Django 裡方便的使用這個地址,比如你的 ImageField 叫 tupian,你可以在 template 中用{{object.tupian.url}}。
舉個例子:假設你的 MEDIA_ROOT=’/home/media’,upload_to 設定為 ‘photos/%Y/%m/%d’,’%Y/%m/%d’ 部分使用strftime() 提供。如果你在 2013年10月10日上傳了一個檔案,那麼它就存在 /home/media/photos/2013/10/10/ 下。
檔案在 model例項 執行 save操作的同時儲存,所以檔案在model例項執行save之前,硬碟的上的檔名的是不可靠的。
注意:要驗證使用者上傳的檔案確實是自己需要的,以防止安全漏洞出現。
預設情況下,FileField 在資料庫中表現為 varchar(100) 的一個列。你可以使用 max_length 來改變這個大小。

11、FileField 和 FieldFile
當你訪問 一個 model 內的 FileField 時,將得到一個 FieldFile 例項來訪問實際的檔案。這個類提供了幾個屬性和方法用來和實際的檔案資料互動:
FieldFile.url:只讀屬性,獲取檔案的相對URL地址;
FieldFile.open( mode = ‘rb’ ):開啟檔案,和python 的 open 一樣;
FieldFile.close():和 python 的 file.close() 一樣;
FieldFile.save( name, content, save=True ):name 是檔名,content 是包含了檔案內容的 django.core.files.File 例項,與 python 的 file 不一樣。The optional save argument controls whether or not the instance is saved after the file has been altered. Defaults to True。
兩種方式 進行 content 設定:
from django.core.files import File
f = open( ‘helo.txt’ )
content = File(f)
另一種是:
from django.core.files.base import ContentFile
content = ContentFile( ‘helloworld’ )
更多內容可見:https://docs.djangoproject.com/en/dev/topics/files/
FieldFile.delete( save = True ):刪除當前的檔案。如果檔案已經開啟,則自動關閉。The optional save argument controls whether or not the instance is saved after the file has been deleted. Defaults to True.
值得注意的是:當一個 model例項 被刪除之後,相關聯的檔案並沒有被刪除,需要自己清除!

12、FloatField
與 python 裡的 float 例項相同,預設的 form widget 是 TextInput。
雖然 FloatField 與 DecimalField 都是表示實數,但卻是不同的表現形式,FloatField 用的是 python d float 型別,但是 DecimalField 用的卻是 Decimal 型別。
13、ImageField
在 FileField 基礎上加上是否是合法圖片驗證功能的一個型別。
除了 FileField 有的屬性外,ImageField 另有 height 和 width 屬性。

注意:需要安裝 PIL 或者 Pillow 模組。在資料庫中同樣表現為 varchar(100),可通過 max_length 改大小。

14、IntegerField
整數,預設的form widget 是 TextInput。

15、IPAddressField
IP地址,字串型別,如 127.0.0.1。預設 form widget 是 TextInput。

16、TextField
大文字,巨長的文字。預設的 form widget 是 Textarea。

17、URLField
加了 URL 合法性驗證的 CharField。
預設的 form widget 是 TextInput。
預設max_length=200,可修改。