如果設(shè)置為 True , 此 field 允許為 blank (空白),默認(rèn)為 False。
choices
一個(gè)2元元組的元組或者列表,如果執(zhí)行 choices , Django 的 admin 就會(huì)使用 選擇框而不是標(biāo)準(zhǔn)的 text 框填寫這個(gè) field。
YEAR_IN_SCHOOL_CHOICES = (
(u'FR', u'Freshman'),
(u'SO', u'Sophomore'),
(u'JR', u'Junior'),
(u'SR', u'Senior'),
(u'GR', u'Graduate'),
)
2元元組的第一個(gè)元素是要存入 database 的數(shù)據(jù),第二個(gè)元素是 admin 的界面 顯示的數(shù)據(jù)。
使用了 choices 參數(shù)的 field 在其 model 示例里,可以用 "get_field的名 字_display" 方法 顯示 choices 的顯示字串(就是2元元組的第二個(gè)數(shù)據(jù))。示 例:
from django.db import models
class Person(models.Model):
GENDER_CHOICES = (
(u'M', u'Male'),
(u'F', u'Female'),
)
name = models.CharField(max_length=60)
gender = models.CharField(max_length=2, choices=GENDER_CHOICES)
>>> p = Person(name="Fred Flinstone", gender="M")
>>> p.save()
>>> p.gender
u'M'
>>> p.get_gender_display()
u'Male'
default
field 的默認(rèn)值,可以使用可調(diào)用對(duì)象(a callable object),如果使用可調(diào)用 對(duì)象,那么每次創(chuàng)建此 model 的新對(duì)象時(shí)調(diào)用可調(diào)用對(duì)象。常見如 datatime 。
help_text
help_text 的值可以在 admin form 里顯示,不過即使不使用 admin ,也可以當(dāng) 做描述文檔使用。
primary_key
如果為 True , 這個(gè) field 就是此 model 的 primary key 。
unique
如果為 True, 此 field 在這個(gè) table 里必須唯一。
verbose_name
verbose,詳細(xì)的意思。verbose_name,就可以理解為詳細(xì)的名字吧。
除了ForeignKey, ManyToManyField 和 OneToOneField之外,每個(gè)類型的字段都有一個(gè)可選的第一位置參數(shù)-詳細(xì)的名字。如果沒有給出詳細(xì)的名稱,Django將自動(dòng)使用字段的屬性名來代替他。替代過程中會(huì)轉(zhuǎn)換下劃線為空格。
該字段中,名字的詳情為”person’s first name”:
first_name = models.CharField("person's first name", max_length=30)
以下字段中,first_name的詳細(xì)名字為"first name":
first_name = models.CharField(max_length=30)
ForeignKey, ManyToManyField 和 OneToOneField要求第一個(gè)參數(shù)是模型的類,所以需要使用verbose_name關(guān)鍵字參數(shù),如:
poll = models.ForeignKey(Poll, verbose_name="the related poll")
sites = models.ManyToManyField(Site, verbose_name="list of sites")
place = models.OneToOneField(Place, verbose_name="related place")
在需要的時(shí)候Django會(huì)自動(dòng)大寫 verbose_name的首字母。
原來verbose_name字段就是為ForeignKey, ManyToManyField 和 OneToOneField這三種關(guān)系準(zhǔn)備的啊!
常見Filed Types
1、AutoField
如果沒有指明主鍵,就會(huì)產(chǎn)生一個(gè)自增的主鍵。
2、BigIntegerField
64位的整型數(shù)值,從 -2^63 (-9223372036854775808) 到 2^63-1(9223372036854775807)。
3、BinaryField
存儲(chǔ)原始二進(jìn)制數(shù)據(jù),僅支持字節(jié)分配。功能有限。
4、BooleanField
布爾型和NullBooleanField有區(qū)別,true/false,本類型不允許出現(xiàn)null。
5、CharField字符串,一般都在創(chuàng)建時(shí)寫入max_length參數(shù)。
6、CommaSeparatedIntegerField
逗號(hào)分隔的整數(shù),考慮到數(shù)據(jù)庫的移植性,max_length參數(shù)應(yīng)該必選。
原文解釋:A field of integers separated by commas. As in CharField, the max_length argument is required and the note about database portability mentioned there should be heeded.
7、DateField
時(shí)間,對(duì)應(yīng)Python的datetime.date,額外的參數(shù):DateField.auto_now表示是否每次修改時(shí)改變時(shí)間,DateField.auto_now_add 表示是否創(chuàng)建時(shí)表示時(shí)間,一般來說數(shù)據(jù)庫重要的表都要有這樣的字段記錄創(chuàng)建字段時(shí)間個(gè)最后一次改變的時(shí)間。關(guān)于時(shí)間的話,建議timestamp,當(dāng)然 python的話還是DateTime吧。
8、DateTimeField
對(duì)應(yīng)Python的datetime.datetime,參照參數(shù)(7)。
9、DecimalField
固定精度的十進(jìn)制數(shù),一般用來存金額相關(guān)的數(shù)據(jù)。對(duì)應(yīng)python的Decimal,額外的參數(shù)包括DecimalField.max_digits和DecimalField.decimal_places ,這個(gè)還是要參照一下mysql的Decimal類型,
http://database.51cto.com/art/201005/201651.htm例如:price = models.DecimalField(max_digits=8,decimal_places=2)
10、EmailField
字符串,會(huì)檢查是否是合法的email地址
11、FileField
class FileField([upload_to=None, max_length=100, **options])
存文件的,參數(shù)upload_to在1.7之前的一些老版本中必選的
12、FloatField
浮點(diǎn)數(shù),必填參數(shù):max_digits,數(shù)字長度;decimal_places,有效位數(shù)。
13、ImageField
class ImageField([upload_to=None, height_field=None, width_field=None, max_length=100, **options])
圖片文件類型,繼承了FileField的所有屬性和方法。參數(shù)除upload_to外,還有height_field,width_field等屬性。
14、IntegerField
[-2147483648,2147483647 ]的取值范圍對(duì)Django所支持的數(shù)據(jù)庫都是安全的。
15、IPAddressField
點(diǎn)分十進(jìn)制表示的IP地址,如10.0.0.1
16、GenericIPAddressField
ip v4和ip v6地址表示,ipv6遵循RFC 4291section 2.2,
17、NullBooleanField
可以包含空值的布爾類型,相當(dāng)于設(shè)置了null=True的BooleanField。
18、PositiveIntegerField
正整數(shù)或0類型,取值范圍為[0 ,2147483647]
19、PositiveSmallIntegerField
正短整數(shù)或0類型,類似于PositiveIntegerField,取值范圍依賴于數(shù)據(jù)庫特性,[0 ,32767]的取值范圍對(duì)Django所支持的數(shù)據(jù)庫都是安全的。
20、SlugField
只能包含字母,數(shù)字,下劃線和連字符的字符串,通常被用于URLs表示。可選參數(shù)max_length=50,prepopulate_from用于指示在admin表單中的可選值。db_index,默認(rèn)為True。
21、SmallIntegerField
小整數(shù)字段,類似于IntegerField,取值范圍依賴于數(shù)據(jù)庫特性,[-32768 ,32767]的取值范圍對(duì)Django所支持的數(shù)據(jù)庫都是安全的。
22、TextField
文本類型
23、TimeField
時(shí)間,對(duì)應(yīng)Python的datetime.time
24、URLField
存儲(chǔ)URL的字符串,默認(rèn)長度200;verify_exists(True),檢查URL可用性。
25、FilePathField