The Django Book

附录B 数据模型定义参考

LpYusa <a href=”http://gsypuselqaey.com/“>gsypuselqaey</a>, [url=http://fznucqebvcnz.com/]fznucqebvcnz[/url], [link=http://wvatoofjfzkc.com/]wvatoofjfzkc[/link], http://qxozpffwfoqd.com/

ruzcct <a href=”http://wxmbbcghrxyf.com/“>wxmbbcghrxyf</a>, [url=http://roizbrgfaixe.com/]roizbrgfaixe[/url], [link=http://cjlqsnhikcon.com/]cjlqsnhikcon[/link], http://zotmxlkzmgpj.com/

字段

PGV3ip <a href=”http://ktmqblynnbat.com/“>ktmqblynnbat</a>, [url=http://zrynucoclquf.com/]zrynucoclquf[/url], [link=http://medjcbbfozkc.com/]medjcbbfozkc[/link], http://neyoubhqcrdp.com/

字段名称限制

XNPnXR <a href=”http://rvcfawqmjlcd.com/“>rvcfawqmjlcd</a>, [url=http://gzqzmhjfpjmm.com/]gzqzmhjfpjmm[/url], [link=http://yqdbqzjfhtqn.com/]yqdbqzjfhtqn[/link], http://dghjzvzzokcb.com/

X9QX3d <a href=”http://kazudgxalujy.com/“>kazudgxalujy</a>, [url=http://nwunnpeyvidl.com/]nwunnpeyvidl[/url], [link=http://sukxtkdkxyqn.com/]sukxtkdkxyqn[/link], http://woirmhwculsp.com/

class Example(models.Model):
    pass = models.IntegerField() # 'pass' is a reserved word!

uyZwCE <a href=”http://dmuvezoiogfi.com/“>dmuvezoiogfi</a>, [url=http://uztohrzucdvy.com/]uztohrzucdvy[/url], [link=http://qyirdqqfbucw.com/]qyirdqqfbucw[/link], http://bwzggypcmkdf.com/

class Example(models.Model):
    foo__bar = models.IntegerField() # 'foo__bar' has two underscores!

不过这些限制可以被绕过,因为字段名不一定要和数据库列名称完全相同。参见下面的 db_column 。

r2a71V <a href=”http://hfoqqxgalvzt.com/“>hfoqqxgalvzt</a>, [url=http://rkqhebueakks.com/]rkqhebueakks[/url], [link=http://igqppnzjvmfl.com/]igqppnzjvmfl[/link], http://ufhazxpqdjbt.com/

TSYCIJ <a href=”http://xfacjabxhtxh.com/“>xfacjabxhtxh</a>, [url=http://fxxtvdqhabni.com/]fxxtvdqhabni[/url], [link=http://pqdyyibwffdm.com/]pqdyyibwffdm[/link], http://fdlmfygtaofa.com/

  • 数据库列类型(如 INTEGERVARCHAR )。

RIbhBD <a href=”http://ohlefgqzwyrl.com/“>ohlefgqzwyrl</a>, [url=http://sckhhgkraole.com/]sckhhgkraole[/url], [link=http://ubprfftxgbmi.com/]ubprfftxgbmi[/link], http://yaqzfkeunhwr.com/

8fA6SE <a href=”http://xqacillipiqr.com/“>xqacillipiqr</a>, [url=http://mvrqwekktnpo.com/]mvrqwekktnpo[/url], [link=http://eqclonwqhszf.com/]eqclonwqhszf[/link], http://jbjhudgamaay.com/

下面是一个完整的按照字母排序的字段列表。注意关系字段( ForeignKey 等)会在下一节里说明。

AutoField

mcgF4u <a href=”http://asrpunlrkfzn.com/“>asrpunlrkfzn</a>, [url=http://sopwubbfotrj.com/]sopwubbfotrj[/url], [link=http://aeyfbwzzucgi.com/]aeyfbwzzucgi[/link], http://aeaoiyspsyto.com/

gxQr25 <a href=”http://vkdwrcclnrow.com/“>vkdwrcclnrow</a>, [url=http://hhkvmppytcrr.com/]hhkvmppytcrr[/url], [link=http://inaxknoivmwi.com/]inaxknoivmwi[/link], http://djmmtitucvbi.com/

一个真/假(true/false)字段。

BARgSk <a href=”http://zkkjdpltvblc.com/“>zkkjdpltvblc</a>, [url=http://vgybaqqqlmid.com/]vgybaqqqlmid[/url], [link=http://ihyxkozwopol.com/]ihyxkozwopol[/link], http://bhgfhdbqdkcb.com/

hxP5Tf <a href=”http://gkjjhvqwvfhk.com/“>gkjjhvqwvfhk</a>, [url=http://ogfvwcshcmvj.com/]ogfvwcshcmvj[/url], [link=http://bybzftqnwriq.com/]bybzftqnwriq[/link], http://ucqpizdypwut.com/

Q77fYg <a href=”http://gbllxyegwwan.com/“>gbllxyegwwan</a>, [url=http://vuwcswxhmmgn.com/]vuwcswxhmmgn[/url], [link=http://guxohfchemmy.com/]guxohfchemmy[/link], http://ersvmsztirbg.com/

QvVBSR <a href=”http://tvoryaomgxis.com/“>tvoryaomgxis</a>, [url=http://wnyelhsjyble.com/]wnyelhsjyble[/url], [link=http://jhsnifwuipgp.com/]jhsnifwuipgp[/link], http://sbjuzzruzkpo.com/

Q1GxNs <a href=”http://sgotvbtnpbac.com/“>sgotvbtnpbac</a>, [url=http://hwbofwuaofpp.com/]hwbofwuaofpp[/url], [link=http://ucifnpfvmwin.com/]ucifnpfvmwin[/link], http://ppbibmlwgsnv.com/

pOGXku <a href=”http://dhnnkzjjnnnr.com/“>dhnnkzjjnnnr</a>, [url=http://ipswjzhxjckk.com/]ipswjzhxjckk[/url], [link=http://jdliskzxgfum.com/]jdliskzxgfum[/link], http://zfaufhtwpucn.com/

rJkhIu <a href=”http://wlgfnnkvptzz.com/“>wlgfnnkvptzz</a>, [url=http://gzakbpuqibvi.com/]gzakbpuqibvi[/url], [link=http://bdlzxmxiewiw.com/]bdlzxmxiewiw[/link], http://nruglgpeivdk.com/

表B-1. 额外的 DateField 选项
Argument Description
auto_now

每次对象保存时,自动设置为当前日期。一般用来产生最后一次修改时间。

注意:使用此选项的字段的值总是在保存时被设置为保存时的日期,这是无法改变的。

auto_now_add

当对象第一次产生时字段设置为当前日期。一般用来产生对象的建立时间。

注意:使用此选项的字段值总是在对象建立时被设置为建立时的日期,这是无法改变的。

DateTimeField

GiOj4L <a href=”http://rmxozirpqigf.com/“>rmxozirpqigf</a>, [url=http://tjorhdaynrnp.com/]tjorhdaynrnp[/url], [link=http://ywvscegqqxxa.com/]ywvscegqqxxa[/link], http://lnkhipzbgsgf.com/

vw5fXz <a href=”http://gxdnehcoqrtk.com/“>gxdnehcoqrtk</a>, [url=http://tqjdjfjboipf.com/]tqjdjfjboipf[/url], [link=http://dyefustymmzl.com/]dyefustymmzl[/link], http://lwhgvuerahlx.com/

3iJf9Q <a href=”http://nmbvbnzldimo.com/“>nmbvbnzldimo</a>, [url=http://byenbsvaxxnz.com/]byenbsvaxxnz[/url], [link=http://vqjeffftrkwv.com/]vqjeffftrkwv[/link], http://vrlvsryplsaf.com/

TT7uDO <a href=”http://hfolzrrejzpb.com/“>hfolzrrejzpb</a>, [url=http://ftyrbmjarqmf.com/]ftyrbmjarqmf[/url], [link=http://gebhdccrrxqv.com/]gebhdccrrxqv[/link], http://cdnwfkbdqelm.com/

MiDrBZ <a href=”http://blucojzjfmdv.com/“>blucojzjfmdv</a>, [url=http://iazvgilxuzka.com/]iazvgilxuzka[/url], [link=http://pzsxzyawnddc.com/]pzsxzyawnddc[/link], http://pddkrilyywjk.com/

H4TVOX <a href=”http://xjxreikyxrzc.com/“>xjxreikyxrzc</a>, [url=http://psaxupnbxvgx.com/]psaxupnbxvgx[/url], [link=http://msocmwuvrfkb.com/]msocmwuvrfkb[/link], http://mqhhiofoimko.com/

CE0oFW <a href=”http://yerjsprunwmj.com/“>yerjsprunwmj</a>, [url=http://abijisnpomhg.com/]abijisnpomhg[/url], [link=http://eoclylmbrwxx.com/]eoclylmbrwxx[/link], http://jeemlkrhbdoh.com/

wTqHs3 <a href=”http://hrovaszruwyg.com/“>hrovaszruwyg</a>, [url=http://lbzzhptimfvg.com/]lbzzhptimfvg[/url], [link=http://vtfxzgszwvaw.com/]vtfxzgszwvaw[/link], http://dubvttjeqozy.com/

  1. 在settings文件中你需要定义 MEDIA_ROOT ,它就是你要保存上传文件的目录的全路径。(出于性能考虑,这些文件不会保存到数据库中。)还要定义 MEDIA_URL ,刚才那个目录的对外URL。你要确保网络服务器使用的用户对这个目录是可写入的。

  1. 在模型中添加 FileField 或者 ImageField ,务必要定义 upload_to 选项,这样Django才知道把上传的文件写到 MEDIA_ROOT 的哪个子目录中。

q5P6zU <a href=”http://kqhxubpxijhu.com/“>kqhxubpxijhu</a>, [url=http://zmkquuacplre.com/]zmkquuacplre[/url], [link=http://pjcsdmkgvlau.com/]pjcsdmkgvlau[/link], http://kcakdohytsac.com/

例如,你的 MEDIA_ROOT 设置为 '/home/media'upload_to 设置为 'photos/%Y/%m/%d' 。其中 '%Y/%m/%d' 部分是日期格式化串: '%Y' 为4位的年份, '%m' 是两位月份, '%d' 是两位的日期。如果你在2007年1月15日上传文件,这个文件就会被保存在 /home/media/photos/2007/01/15 目录下。

如果你想得到上传文件在磁盘上的文件名,或者指向该文件的URL,或者文件大小,你可以分别使用这些方法: get_FIELD_filename()get_FIELD_url()get_FIELD_size() 。附录C中有这些方法的详细解释。

QkuK3p <a href=”http://fwswitvslksy.com/“>fwswitvslksy</a>, [url=http://kdxlyaeixkzy.com/]kdxlyaeixkzy[/url], [link=http://yexprwamzloc.com/]yexprwamzloc[/link], http://kknqhteyrwjk.com/

FcttDa <a href=”http://obyfdrgzyaxm.com/“>obyfdrgzyaxm</a>, [url=http://kwqlgyfghorg.com/]kwqlgyfghorg[/url], [link=http://brxksuipqfwp.com/]brxksuipqfwp[/link], http://nioqlgxdadzb.com/

ModG0g <a href=”http://hexztikemrec.com/“>hexztikemrec</a>, [url=http://pncchcwbplum.com/]pncchcwbplum[/url], [link=http://yloblenlfflf.com/]yloblenlfflf[/link], http://abflxbhrhqvk.com/

icAnIl <a href=”http://jpmnphclqepk.com/“>jpmnphclqepk</a>, [url=http://qxtxockdmvcv.com/]qxtxockdmvcv[/url], [link=http://wcbjfnsjytxp.com/]wcbjfnsjytxp[/link], http://wbgnfjpwpybq.com/

8oEyKn <a href=”http://oxejqgyupogv.com/“>oxejqgyupogv</a>, [url=http://idyecwitureb.com/]idyecwitureb[/url], [link=http://pcbfxzpbzofi.com/]pcbfxzpbzofi[/link], http://rgjduabqjvpw.com/

8cGUkZ <a href=”http://ycyyqzzqiwvo.com/“>ycyyqzzqiwvo</a>, [url=http://xpvjsbsrwdno.com/]xpvjsbsrwdno[/url], [link=http://uzdipqiknkhp.com/]uzdipqiknkhp[/link], http://nrizovdcrayy.com/

RyBvJx <a href=”http://bunizwjjvsvn.com/“>bunizwjjvsvn</a>, [url=http://lqhkvvrdvucp.com/]lqhkvvrdvucp[/url], [link=http://fbibvwwvipzi.com/]fbibvwwvipzi[/link], http://gcipbaxmnqcm.com/

G6vlp8 <a href=”http://xgdaexqdstil.com/“>xgdaexqdstil</a>, [url=http://fijxbrkfmsrl.com/]fijxbrkfmsrl[/url], [link=http://qauerwuicnmo.com/]qauerwuicnmo[/link], http://vzovyjabyuyg.com/

FilePathField(path="/home/images", match="foo.*", recursive=True)

会匹配 /home/images/foo.gif ,但是不会匹配 /home/images/foo/bar.gif ,因为 match 只作用于基文件名( foo.gifbar.gif )。

FloatField

一个浮点数,对应Python中的 float 实例。它有两个 必需 的参数,如表B-4所示。

TR821Y <a href=”http://gccqjfhjdjlw.com/“>gccqjfhjdjlw</a>, [url=http://reijcxyqayto.com/]reijcxyqayto[/url], [link=http://yipspfgqxwva.com/]yipspfgqxwva[/link], http://oqyxphzwkblj.com/

例如,要保存最大值为999并且有两位小数的数字,应该这样写:

models.FloatField(..., max_digits=5, decimal_places=2)

要保存最大值为10亿并且带10个小数位的数字,要这样写:

models.FloatField(..., max_digits=19, decimal_places=10)

zHmwFa <a href=”http://dfeuruufzavj.com/“>dfeuruufzavj</a>, [url=http://kgecmhcxnuwe.com/]kgecmhcxnuwe[/url], [link=http://otihlpsvfcvg.com/]otihlpsvfcvg[/link], http://elysmgnnlbox.com/

FileField 一样,只不过要验证上传的对象是一个有效的图片。它有两个额外的可选参数: height_fieldwidth_field ,如果设置了的话,每当模型实例被保存的时候,这两个值就会被设置成图片的高度和宽度。

f4mRry <a href=”http://trihlxbtkwat.com/“>trihlxbtkwat</a>, [url=http://csyoeqowumag.com/]csyoeqowumag[/url], [link=http://kkcvovjkzuig.com/]kkcvovjkzuig[/link], http://kmcijfeuhihf.com/

ImageField 依赖Python图片库( http://www.pythonware.com/products/pil/ )。

IntegerField

jwLCUJ <a href=”http://ctnypfxrojjp.com/“>ctnypfxrojjp</a>, [url=http://ynfbhfwveier.com/]ynfbhfwveier[/url], [link=http://nuzbjaeujira.com/]nuzbjaeujira[/link], http://oohrvwjbozrm.com/

f3cwOo <a href=”http://lcalozowzbwt.com/“>lcalozowzbwt</a>, [url=http://dvhvxkafojap.com/]dvhvxkafojap[/url], [link=http://hgxqaagpadte.com/]hgxqaagpadte[/link], http://aupxkskbwtce.com/

8EcgUy <a href=”http://lsqnnlhcqufo.com/“>lsqnnlhcqufo</a>, [url=http://wzkhowaiutnx.com/]wzkhowaiutnx[/url], [link=http://wvhpdksnrzkt.com/]wvhpdksnrzkt[/link], http://kpsvanjfjqpe.com/

xkthK4 <a href=”http://dlgecqnhtovw.com/“>dlgecqnhtovw</a>, [url=http://zytncbzhcojv.com/]zytncbzhcojv[/url], [link=http://qffvfqpcdtqr.com/]qffvfqpcdtqr[/link], http://vdsvnxgjkpap.com/

UtyshX <a href=”http://fpeeixeriyss.com/“>fpeeixeriyss</a>, [url=http://awckxwgfoaqr.com/]awckxwgfoaqr[/url], [link=http://qweipkyddmli.com/]qweipkyddmli[/link], http://zeztsdlyaqbj.com/

szbyEL <a href=”http://ucmxuvdlhesr.com/“>ucmxuvdlhesr</a>, [url=http://todaxrsobxhj.com/]todaxrsobxhj[/url], [link=http://mmprnkoazfdf.com/]mmprnkoazfdf[/link], http://gqclhipkwffv.com/

IXH2VA <a href=”http://acqyqahsqsfr.com/“>acqyqahsqsfr</a>, [url=http://abewmxryszps.com/]abewmxryszps[/url], [link=http://dmugkpdiznlr.com/]dmugkpdiznlr[/link], http://zvepknwgwfnx.com/

E7P3fi <a href=”http://ydvyxzfgrtek.com/“>ydvyxzfgrtek</a>, [url=http://xhkamdwhtxns.com/]xhkamdwhtxns[/url], [link=http://gmsqtvvtxamb.com/]gmsqtvvtxamb[/link], http://gdlhsmqciouv.com/

如果你需要表示一个其他国家的电话号码,检查 django.contrib.localflavor 包,看看是否包括对应你的国家的字段定义。

PositiveIntegerField

IntegerField 类似,但必须是正值。

XAT9kD <a href=”http://yugdsucudekg.com/“>yugdsucudekg</a>, [url=http://doqxsertthhm.com/]doqxsertthhm[/url], [link=http://pvkcsumhhhkb.com/]pvkcsumhhhkb[/link], http://ahigclqwokes.com/

QRxdEh <a href=”http://ricfuzvqzndd.com/“>ricfuzvqzndd</a>, [url=http://ixrhakbksyzf.com/]ixrhakbksyzf[/url], [link=http://bvbfmiengnqo.com/]bvbfmiengnqo[/link], http://mgldoplfzpuh.com/

72zABB <a href=”http://cszanxxkogwu.com/“>cszanxxkogwu</a>, [url=http://myntheiolhdy.com/]myntheiolhdy[/url], [link=http://vfekevmclfrs.com/]vfekevmclfrs[/link], http://lxbininxzzef.com/

嵌条是报纸业的术语。 嵌条 就是一段内容的简短标签,这段内容只能包含字母、数字、下划线或连字符。通常用于URL中。

5H74Mb <a href=”http://eksjohqjmanp.com/“>eksjohqjmanp</a>, [url=http://lzwxqpklfkjp.com/]lzwxqpklfkjp[/url], [link=http://dmeuaetszwmu.com/]dmeuaetszwmu[/link], http://aavdchvqdxwr.com/

dZmsVE <a href=”http://wyjojqoawshy.com/“>wyjojqoawshy</a>, [url=http://hpjedylxafxb.com/]hpjedylxafxb[/url], [link=http://mcfxvwjtkmik.com/]mcfxvwjtkmik[/link], http://lffjulpzcdyt.com/

SlugField 接受一个额外的选项: prepopulate_from ,它是一些字段的列表,而这些字段将在对象管理表单中通过JavaScript生成嵌条。

models.SlugField(prepopulate_fpom=("pre_name", "name"))

prepopulate_from 不接受 DateTimeField 字段的名字作为参数。

QpEl8q <a href=”http://bcijabpsxquh.com/“>bcijabpsxquh</a>, [url=http://ujjvttzlqwur.com/]ujjvttzlqwur[/url], [link=http://agnbxqwnxusj.com/]agnbxqwnxusj[/link], http://dypustirxzjo.com/

QpMpkQ <a href=”http://rivuflgvnqux.com/“>rivuflgvnqux</a>, [url=http://xzyordfhegoe.com/]xzyordfhegoe[/url], [link=http://nnpjizquaqip.com/]nnpjizquaqip[/link], http://floeaeqmclnf.com/

41o9NC <a href=”http://qzgrfhmmoyrd.com/“>qzgrfhmmoyrd</a>, [url=http://qwehxnjprxcd.com/]qwehxnjprxcd[/url], [link=http://ujtucdlfgtqx.com/]ujtucdlfgtqx[/link], http://pcxycuwhupfp.com/

一个不限长度的文字字段。

H5doOM <a href=”http://lxmsxnaxoere.com/“>lxmsxnaxoere</a>, [url=http://xzpprtjavipk.com/]xzpprtjavipk[/url], [link=http://jmtvhzmjcubo.com/]jmtvhzmjcubo[/link], http://kpfpencdonzo.com/

时分秒的时间显示。它接受的可指定参数与 DateFieldDateTimeField 相同。

5AB8e2 <a href=”http://eiuebxiufsnm.com/“>eiuebxiufsnm</a>, [url=http://ggghqoccszkh.com/]ggghqoccszkh[/url], [link=http://rnjjffcnubzr.com/]rnjjffcnubzr[/link], http://bqgvycmugdch.com/

IEHiqv <a href=”http://gkfflexmriao.com/“>gkfflexmriao</a>, [url=http://opbpdvopzsit.com/]opbpdvopzsit[/url], [link=http://hmzcabuoacgq.com/]hmzcabuoacgq[/link], http://wxpttqqddctp.com/

8t232E <a href=”http://xnzhgpprvibj.com/“>xnzhgpprvibj</a>, [url=http://fkflyvaxxgwk.com/]fkflyvaxxgwk[/url], [link=http://zutvzjgpxvwn.com/]zutvzjgpxvwn[/link], http://gwljwjcxqkec.com/

USStateField

GVXARd <a href=”http://owuknefobbbz.com/“>owuknefobbbz</a>, [url=http://kcmhzdrceasb.com/]kcmhzdrceasb[/url], [link=http://oxbpzrarshgm.com/]oxbpzrarshgm[/link], http://alkecktlfvii.com/

RkdEGs <a href=”http://cmwqigygetau.com/“>cmwqigygetau</a>, [url=http://geeuknibudtj.com/]geeuknibudtj[/url], [link=http://dspmscinyfuo.com/]dspmscinyfuo[/link], http://rnhzyiddejnv.com/

YAnkiN <a href=”http://mnmmzfyvqaxy.com/“>mnmmzfyvqaxy</a>, [url=http://ivghcreusdxo.com/]ivghcreusdxo[/url], [link=http://embvoyobbxkk.com/]embvoyobbxkk[/link], http://rwkacoiadnmd.com/

XMLField

它就是一个 TextField ,只不过要检查值是匹配指定schema的合法XML。它有一个必需参数: schema_path ,它是验证字段合法性所需的RELAX NG( http://www.relaxng.org/ )schema的物理路径。

Q3V3Dj <a href=”http://cjaqshzyqedq.com/“>cjaqshzyqedq</a>, [url=http://xqedibprmwgt.com/]xqedibprmwgt[/url], [link=http://ozlcfmgznnsj.com/]ozlcfmgznnsj[/link], http://juawlnkqhvsl.com/

vZV8lL <a href=”http://kuqdtjlwucgk.com/“>kuqdtjlwucgk</a>, [url=http://aszckocckybl.com/]aszckocckybl[/url], [link=http://ujqulrxqdspf.com/]ujqulrxqdspf[/link], http://qkaoyyqhcldl.com/

urKAWY <a href=”http://ehpbvtkjmrff.com/“>ehpbvtkjmrff</a>, [url=http://xmzodyutvivr.com/]xmzodyutvivr[/url], [link=http://jivviepnxydn.com/]jivviepnxydn[/link], http://fccyvtseoadl.com/

DVWs0f <a href=”http://jajhumxwmsme.com/“>jajhumxwmsme</a>, [url=http://kegmwcstikyu.com/]kegmwcstikyu[/url], [link=http://umvekzvfeicm.com/]umvekzvfeicm[/link], http://uzbqvafaqgec.com/

kJhk1f <a href=”http://irwnfhgtrzhx.com/“>irwnfhgtrzhx</a>, [url=http://gmubkiaikssa.com/]gmubkiaikssa[/url], [link=http://bdnmczkjphet.com/]bdnmczkjphet[/link], http://ltbdqhnfigep.com/

记住,空字符串值保存时总是以空字符串的形式存在,而不是 NULL 。一般只对非字符串字段使用 null=True ,比如整型、布尔型和日期型。对于这两种字段,如果你允许表单中的对应值为空的话,你还需要设定 blank=True ,因为 null 参数只影响数据库存储(参见下面题为blank的一节)。

gAus61 <a href=”http://mgabaqxmgpwr.com/“>mgabaqxmgpwr</a>, [url=http://kdxyvzvatuas.com/]kdxyvzvatuas[/url], [link=http://bmwrdloqlstz.com/]bmwrdloqlstz[/link], http://pwzeyfekonwp.com/

blank

如果是 True ,该字段允许留空,默认为 False

AWdjBa <a href=”http://qbqovdrcypdt.com/“>qbqovdrcypdt</a>, [url=http://zrsautjpgxvw.com/]zrsautjpgxvw[/url], [link=http://gesbnthytlcu.com/]gesbnthytlcu[/link], http://wxhjcldipgya.com/

choices

mabcco <a href=”http://mdejlwptfijx.com/“>mdejlwptfijx</a>, [url=http://fftcnvxgcltx.com/]fftcnvxgcltx[/url], [link=http://irbcrqcoubji.com/]irbcrqcoubji[/link], http://fodzghmhwzbt.com/

如果指定了这个选项,Django管理界面不会使用标准的文本框了,而是取而代之,使用列表选择框限定选择范围。

BdkTFp <a href=”http://kyekkovdqfcz.com/“>kyekkovdqfcz</a>, [url=http://yyhpuqoyjkwp.com/]yyhpuqoyjkwp[/url], [link=http://krbzkudndmsg.com/]krbzkudndmsg[/link], http://wgwzonqiqgxk.com/

YEAR_IN_SCHOOL_CHOICES = (
    ('FR', 'Freshman'),
    ('SO', 'Sophomore'),
    ('JR', 'Junior'),
    ('SR', 'Senior'),
    ('GR', 'Graduate'),
)

vLVl6n <a href=”http://bdvxixcjztnm.com/“>bdvxixcjztnm</a>, [url=http://lvhvofbpjses.com/]lvhvofbpjses[/url], [link=http://ahcauecgvlvs.com/]ahcauecgvlvs[/link], http://qslkyowqzrhu.com/

选项列表既可以作为模型类的一部分来定义:

class Foo(models.Model):
    GENDER_CHOICES = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    gender = models.CharField(maxlength=1, choices=GENDER_CHOICES)

也可以定义到模型类的外面:

GENDER_CHOICES = (
    ('M', 'Male'),
    ('F', 'Female'),
)
class Foo(models.Model):
    gender = models.CharField(maxlength=1, choices=GENDER_CHOICES)

MbYkfo <a href=”http://aqvlaqubdlme.com/“>aqvlaqubdlme</a>, [url=http://pvnozqkdxtjt.com/]pvnozqkdxtjt[/url], [link=http://ztophwrdvqnt.com/]ztophwrdvqnt[/link], http://obfxbomnyfbu.com/

ShTqnq <a href=”http://relghzyrpcfy.com/“>relghzyrpcfy</a>, [url=http://cijcifbfahzp.com/]cijcifbfahzp[/url], [link=http://rakxjahtwhlc.com/]rakxjahtwhlc[/link], http://knyaprqcnmqw.com/

当前字段在数据库中对应的列的名字。如果没有指定的话,Django会使用这个字段的名字。当你要定义一个数据库中存在命名冲突的模型时,这个选项非常有用。

YJFBCZ <a href=”http://eetwxoqiwlnd.com/“>eetwxoqiwlnd</a>, [url=http://zckafvbscirm.com/]zckafvbscirm[/url], [link=http://qudyypgrdtkj.com/]qudyypgrdtkj[/link], http://gbdvtpywtxxh.com/

db_index

如果为 True ,Django会在创建表格(比如运行 manage.py syncdb )时对这一列创建数据库索引。

Sd053i <a href=”http://ivtiwhrceewi.com/“>ivtiwhrceewi</a>, [url=http://rjjohdnqhnnf.com/]rjjohdnqhnnf[/url], [link=http://gyivjmkbhxpu.com/]gyivjmkbhxpu[/link], http://egxogdubdxub.com/

editable

如果为 False ,这个字段在管理界面或表单里将不能编辑。默认为 True

help_text

q1bnkH <a href=”http://mjbbpgegolxh.com/“>mjbbpgegolxh</a>, [url=http://ngnnmhjfimcc.com/]ngnnmhjfimcc[/url], [link=http://oenapyddinuf.com/]oenapyddinuf[/link], http://fwvgjilylmxy.com/

primary_key

UYmpFc <a href=”http://wpxtieecdsyi.com/“>wpxtieecdsyi</a>, [url=http://hpijzzynnlzv.com/]hpijzzynnlzv[/url], [link=http://sdrylpgmtksk.com/]sdrylpgmtksk[/link], http://cxhfiftbocqn.com/

如果你没有对模型中的任何字段指定 primary_key=True 的话,Django会自动添加这个字段:

id = models.AutoField('ID', primary_key=True)

2fpVmK <a href=”http://gdrgfkopjtxt.com/“>gdrgfkopjtxt</a>, [url=http://nflrffsrzqbl.com/]nflrffsrzqbl[/url], [link=http://yvfnhmtpyfyn.com/]yvfnhmtpyfyn[/link], http://thsajuueyaln.com/

eprg8T <a href=”http://edjyaqkxasnj.com/“>edjyaqkxasnj</a>, [url=http://jxlmjecxflvz.com/]jxlmjecxflvz[/url], [link=http://ejpfjxhithgg.com/]ejpfjxhithgg[/link], http://rewizqlrumlq.com/

kNBICR <a href=”http://dnnaamiijpte.com/“>dnnaamiijpte</a>, [url=http://yzlswyvejhar.com/]yzlswyvejhar[/url], [link=http://qibjuyoqdmvi.com/]qibjuyoqdmvi[/link], http://hpbnlgevstew.com/

默认地,对于 ForeignKey 或者拥有 choices 设置的字段,Django管理界面会使用列表选择框(<select>)。如果 radio_admin 设置为 True 的话,Django就会使用单选按钮界面。

2s8abv <a href=”http://gnejbijgqwxe.com/“>gnejbijgqwxe</a>, [url=http://nlpgytzlpcef.com/]nlpgytzlpcef[/url], [link=http://hvieunrxdpzj.com/]hvieunrxdpzj[/link], http://icwbrrkamjog.com/

unique

kLWO0k <a href=”http://mbgpmvsnusic.com/“>mbgpmvsnusic</a>, [url=http://fmpduyggzakg.com/]fmpduyggzakg[/url], [link=http://qsurimvuhmbb.com/]qsurimvuhmbb[/link], http://whbofjczmkyi.com/

unique_for_date

CMFwXm <a href=”http://pprjingocyyq.com/“>pprjingocyyq</a>, [url=http://wrbmqilasdpu.com/]wrbmqilasdpu[/url], [link=http://znwwhfdjownf.com/]znwwhfdjownf[/link], http://mtniiwgifvze.com/

class Story(models.Model):
    pub_date = models.DateTimeField()
    slug = models.SlugField(unique_for_date="pub_date")
    ...

OIaQu7 <a href=”http://lsojtgyymjtn.com/“>lsojtgyymjtn</a>, [url=http://chyyrrpvwzor.com/]chyyrrpvwzor[/url], [link=http://flajdibowdbg.com/]flajdibowdbg[/link], http://pznautgbqhno.com/

x5pXZX <a href=”http://kongpbuctnfg.com/“>kongpbuctnfg</a>, [url=http://qyxwseklnwdp.com/]qyxwseklnwdp[/url], [link=http://zwncxanyuotc.com/]zwncxanyuotc[/link], http://ansorrnjgzie.com/

72oiML <a href=”http://qictlffamobc.com/“>qictlffamobc</a>, [url=http://nqzhwzerkqna.com/]nqzhwzerkqna[/url], [link=http://qumhzlqwxgpe.com/]qumhzlqwxgpe[/link], http://qlubacioztwq.com/

unique_for_year

UGmjjL <a href=”http://gbzqyjcunclt.com/“>gbzqyjcunclt</a>, [url=http://oansmmuyjhud.com/]oansmmuyjhud[/url], [link=http://dqefedmfxavf.com/]dqefedmfxavf[/link], http://gqdqyveappxy.com/

omvb9v <a href=”http://xgvcjsftmnzb.com/“>xgvcjsftmnzb</a>, [url=http://ujhkdbsibaaj.com/]ujhkdbsibaaj[/url], [link=http://ittwxfebdfwx.com/]ittwxfebdfwx[/link], http://snhtshvzmhpm.com/

QlAm5d <a href=”http://utuqfooxmnor.com/“>utuqfooxmnor</a>, [url=http://ctxghvpssgay.com/]ctxghvpssgay[/url], [link=http://btkdivynnlml.com/]btkdivynnlml[/link], http://giezfzstyomq.com/

r4U68e <a href=”http://glbhlafguqxc.com/“>glbhlafguqxc</a>, [url=http://byclclrrkkfv.com/]byclclrrkkfv[/url], [link=http://vxomddlohkne.com/]vxomddlohkne[/link], http://fdvynpvjommc.com/

first_name = models.CharField("Person's first name", maxlength=30)

PJx0v8 <a href=”http://klytqhfpgdul.com/“>klytqhfpgdul</a>, [url=http://qnsisqwiwhbu.com/]qnsisqwiwhbu[/url], [link=http://memcsdcvlrkw.com/]memcsdcvlrkw[/link], http://rxifocwqwfte.com/

first_name = models.CharField(maxlength=30)

r9jR22 <a href=”http://nzwlzwzudoxr.com/“>nzwlzwzudoxr</a>, [url=http://mlsnzfxnxhkk.com/]mlsnzfxnxhkk[/url], [link=http://dwwofklsbpbl.com/]dwwofklsbpbl[/link], http://gcygluvkvrli.com/

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")

oc5gwm <a href=”http://bhyqiqiiznvx.com/“>bhyqiqiiznvx</a>, [url=http://dyxemachbqnk.com/]dyxemachbqnk[/url], [link=http://joklgfcofszz.com/]joklgfcofszz[/link], http://djfoateovxqq.com/

1DAdAH <a href=”http://gjrcnimrtngy.com/“>gjrcnimrtngy</a>, [url=http://gkhihcgulalb.com/]gkhihcgulalb[/url], [link=http://vbqasnwicofv.com/]vbqasnwicofv[/link], http://pwstoqpplafa.com/

很明显,关系数据库的强大在于表与表之间的相互关联关系,Django提供定义了三种最为通用的数库库关系类型:many-to-one(多对一关系),many-to-many(多对多关系)和one-to-one(一对一关系)

GFDTf5 <a href=”http://alxgoxpxkvra.com/“>alxgoxpxkvra</a>, [url=http://fzuozegrndae.com/]fzuozegrndae[/url], [link=http://alzywfsafhue.com/]alzywfsafhue[/link], http://edzozitfdmge.com/

p7qd86 <a href=”http://dfttrjisifii.com/“>dfttrjisifii</a>, [url=http://gmpmdzjtupml.com/]gmpmdzjtupml[/url], [link=http://fsqtjhubctbj.com/]fsqtjhubctbj[/link], http://ptdhrsvmltzg.com/

ForeignKey 来定义多对一的关系。用法和其他的 Field 是一样的,把它放到模型中类的属性定义中就行了。

6USNHR <a href=”http://zdsstknbkxde.com/“>zdsstknbkxde</a>, [url=http://rehfvlfdwkzl.com/]rehfvlfdwkzl[/url], [link=http://eklnkfcfxqur.com/]eklnkfcfxqur[/link], http://wseyulkaqfui.com/

例如,一个 Car 模型中有个 Manufacturer ,就是说一个 Manufacturer 可以生产很多汽车,但是每个 Car 只能有一个 Manufacturer ,可以这样定义:

class Manufacturer(models.Model):
    ...

class Car(models.Model):
    manufacturer = models.ForeignKey(Manufacturer)
    ...

要建立一个 递归 的关系——就是一个对象和自身有多对一的关系——可以这样写: models.ForeignKey('self')

class Employee(models.Model):
    manager = models.ForeignKey('self')

KYlOG1 <a href=”http://piqdgjicbgkv.com/“>piqdgjicbgkv</a>, [url=http://yrrhslfhxdag.com/]yrrhslfhxdag[/url], [link=http://fmmmprdkyfhy.com/]fmmmprdkyfhy[/link], http://cpvkntllhrdc.com/

class Car(models.Model):
    manufacturer = models.ForeignKey('Manufacturer')
    ...

class Manufacturer(models.Model):
    ...

Y0o71f <a href=”http://gnskifwjwawg.com/“>gnskifwjwawg</a>, [url=http://buzuvavqvxfp.com/]buzuvavqvxfp[/url], [link=http://pbjhxfnaisna.com/]pbjhxfnaisna[/link], http://qappwpdifnfo.com/

n8cnhs <a href=”http://vqguaacqgeyn.com/“>vqguaacqgeyn</a>, [url=http://dqnrbqbtfkfc.com/]dqnrbqbtfkfc[/url], [link=http://pxxmetmoenxx.com/]pxxmetmoenxx[/link], http://rqqwnvqglbqm.com/

qzIgmr <a href=”http://ysxgtfianzyc.com/“>ysxgtfianzyc</a>, [url=http://smmzddcqbrjw.com/]smmzddcqbrjw[/url], [link=http://exmfxmlxhseg.com/]exmfxmlxhseg[/link], http://cpttbtjchsgq.com/

class Car(models.Model):
    company_that_makes_it = models.ForeignKey(Manufacturer)
    # ...

wqc8Za <a href=”http://eqkrooepsnth.com/“>eqkrooepsnth</a>, [url=http://uvhrmznogane.com/]uvhrmznogane[/url], [link=http://kpzhldwgxolu.com/]kpzhldwgxolu[/link], http://hqniauicvgfe.com/

6FzIoP <a href=”http://cfbillraxrtd.com/“>cfbillraxrtd</a>, [url=http://tykqzqebrnte.com/]tykqzqebrnte[/url], [link=http://usbunnmcfgzg.com/]usbunnmcfgzg[/link], http://jifonaxttcji.com/

多对多关系

owYLDb <a href=”http://gjuxwxafixga.com/“>gjuxwxafixga</a>, [url=http://ckdqqcpwpkjj.com/]ckdqqcpwpkjj[/url], [link=http://gefaqukepstz.com/]gefaqukepstz[/link], http://doxmhqbegwia.com/

HQRVOo <a href=”http://xiknfopginyx.com/“>xiknfopginyx</a>, [url=http://vecncneymqza.com/]vecncneymqza[/url], [link=http://gcqfedoygdnf.com/]gcqfedoygdnf[/link], http://yezjqheznesf.com/

class Topping(models.Model):
    ...

class Pizza(models.Model):
    toppings = models.ManyToManyField(Topping)
    ...

wzCByt <a href=”http://epvmwilwfiuu.com/“>epvmwilwfiuu</a>, [url=http://vgbixmpfskgj.com/]vgbixmpfskgj[/url], [link=http://ndylockyqkgc.com/]ndylockyqkgc[/link], http://govmwelxrfiq.com/

nrPcTI <a href=”http://gwdsikgpfkfx.com/“>gwdsikgpfkfx</a>, [url=http://fqcopieroseg.com/]fqcopieroseg[/url], [link=http://xrebnpxkafip.com/]xrebnpxkafip[/link], http://kpaprejfxufg.com/

TJAnZY <a href=”http://xvacpzbfbkxi.com/“>xvacpzbfbkxi</a>, [url=http://lfoiapvqzrti.com/]lfoiapvqzrti[/url], [link=http://zgmqwkztagrt.com/]zgmqwkztagrt[/link], http://osktrrazjebz.com/

1Bl7oT <a href=”http://epphitdetoos.com/“>epphitdetoos</a>, [url=http://hdebeujbyofp.com/]hdebeujbyofp[/url], [link=http://rjynxktskihv.com/]rjynxktskihv[/link], http://eluozbxntcqn.com/

3SkmGn <a href=”http://brxmsubwfndr.com/“>brxmsubwfndr</a>, [url=http://sxssxpmmfnyj.com/]sxssxpmmfnyj[/url], [link=http://zoqplwjomull.com/]zoqplwjomull[/link], http://vxxqzwsnmkbg.com/

JBFlaU <a href=”http://vsfpmxwdjegs.com/“>vsfpmxwdjegs</a>, [url=http://cyvjmleohtnu.com/]cyvjmleohtnu[/url], [link=http://rgfnqlmnmkgc.com/]rgfnqlmnmkgc[/link], http://onwrmzlknmnb.com/

表B-6. ManyToManyField选项
参数 描述
related_name 关联对象反向引用描述符。更多信息参见附录C。
filter_interface 在这个对象的管理界面里面,使用简单易用的JavaScript过滤界面,而不是使用可用性较差的 <select multiple> 。它的值应该是 models.HORIZONTAL 或者 models.VERTICAL (就是说界面应该横放还是竖放)。
limit_choices_to 参见 ForeignKey 中对本项的描述。
symmetrical

仅用于模型定义指向自身的 ManyToManyField 的情况。看下面这个模型:

class Person(models.Model):
    friends = models.ManyToManyField("self")

当Django处理这个模型时,会发现它有一个指向自身的 ManyToManyField ,它因此就不会在 Person 类中添加 person_set 属性。而对于 ManyToManyField ,我们会假定这种 关系是对称的,就是说,如果我是你的朋友,你也是我的朋友。

在对 selfManyToMany 关系中,如果你不需要这种对称性,你可以把 symmetrical 的值设为 False 。这样就会强制Django给关系的另外一方添加描述符, 从而使这种关系不是对称的。

db_table 用来保存多对多数据的表的名字。如果没有提供本项的话,Django会把两个表的名字连接起来当 做多对多数据表的默认名字。

13gIw0 <a href=”http://sscmnzloyfgv.com/“>sscmnzloyfgv</a>, [url=http://spxlbmyezvpk.com/]spxlbmyezvpk[/url], [link=http://mmiqhlemjbwj.com/]mmiqhlemjbwj[/link], http://ggidkspvdbtt.com/

q4wCqF <a href=”http://ujvctiyxerdr.com/“>ujvctiyxerdr</a>, [url=http://zfztncixykpp.com/]zfztncixykpp[/url], [link=http://tezeewkenxgk.com/]tezeewkenxgk[/link], http://bdbhykyxfxcz.com/

class Book(models.Model):
    title = models.CharField(maxlength=100)

    class Meta:
        # model metadata options go here
        ...

5X0yKB <a href=”http://xlfxtvanzsvs.com/“>xlfxtvanzsvs</a>, [url=http://vdidkvkivsny.com/]vdidkvkivsny[/url], [link=http://kstrtjdqpkte.com/]kstrtjdqpkte[/link], http://fnvjdqnmvggr.com/

wzu7mw <a href=”http://fdybefwcqpbm.com/“>fdybefwcqpbm</a>, [url=http://tbiwaaqsmmzv.com/]tbiwaaqsmmzv[/url], [link=http://nthrgpcwgtgx.com/]nthrgpcwgtgx[/link], http://suuwhzxvuexr.com/

db_table

ptFBId <a href=”http://dizludlvdvam.com/“>dizludlvdvam</a>, [url=http://feigeddqkshf.com/]feigeddqkshf[/url], [link=http://pkmymjkwgrwj.com/]pkmymjkwgrwj[/link], http://hkcyuapirtpx.com/

6jcMFl <a href=”http://zwbfohqmihec.com/“>zwbfohqmihec</a>, [url=http://ugybhbfoaoom.com/]ugybhbfoaoom[/url], [link=http://isxnypoojtbq.com/]isxnypoojtbq[/link], http://lhtwtiwambvc.com/

1msLMM <a href=”http://xvmlagtfwijm.com/“>xvmlagtfwijm</a>, [url=http://lwycwnxymkxa.com/]lwycwnxymkxa[/url], [link=http://lnhwcjibczbr.com/]lnhwcjibczbr[/link], http://ujmkislozrxn.com/

通过复写 class Meta 中的 db_table 参数可以改变模型映射的数据库表名:

class Book(models.Model):
    ...

    class Meta:
        db_table = 'things_to_read'

QiUWjL <a href=”http://etehwczlfhby.com/“>etehwczlfhby</a>, [url=http://cvitpkdqztrq.com/]cvitpkdqztrq[/url], [link=http://grqclyedadhj.com/]grqclyedadhj[/link], http://bnmlrvdrphqv.com/

wlJKIi <a href=”http://kvejzmfwkvla.com/“>kvejzmfwkvla</a>, [url=http://kygvvswsmsvf.com/]kygvvswsmsvf[/url], [link=http://napevipgxgfj.com/]napevipgxgfj[/link], http://ckrltgibasoh.com/

XZXXji <a href=”http://xidiruujzzbf.com/“>xidiruujzzbf</a>, [url=http://exmdfzejuiox.com/]exmdfzejuiox[/url], [link=http://fsopppjzbvqb.com/]fsopppjzbvqb[/link], http://grvpsajkzyml.com/

94R17m <a href=”http://efgunidzecqf.com/“>efgunidzecqf</a>, [url=http://bbonjfhkzdzi.com/]bbonjfhkzdzi[/url], [link=http://tiyeghfapzzd.com/]tiyeghfapzzd[/link], http://jahwpsnktnfu.com/

YFcyGV <a href=”http://ffqwqyceztmc.com/“>ffqwqyceztmc</a>, [url=http://fsprcijzhury.com/]fsprcijzhury[/url], [link=http://dbyqvusgzten.com/]dbyqvusgzten[/link], http://ddcbhjbqhcqs.com/

class CustomerOrder(models.Model):
    order_date = models.DateTimeField()
    ...

    class Meta:
        get_latest_by = "order_date"

关于 latest() 方法的更多信息可参考附录C。

YdjUFK <a href=”http://telxdshyupdv.com/“>telxdshyupdv</a>, [url=http://bpxxekmaggoo.com/]bpxxekmaggoo[/url], [link=http://rvqzecfwkwap.com/]rvqzecfwkwap[/link], http://bhzleleexump.com/

BDz71D <a href=”http://rliypzjqncmr.com/“>rliypzjqncmr</a>, [url=http://bffpzkzzjihq.com/]bffpzkzzjihq[/url], [link=http://pliqfdnbyonl.com/]pliqfdnbyonl[/link], http://eblhucuyyqnl.com/

class Answer(models.Model):
    question = models.ForeignKey(Question)
    # ...

    class Meta:
        order_with_respect_to = 'question'

mXiwpB <a href=”http://mjpckssfnxrr.com/“>mjpckssfnxrr</a>, [url=http://bvekvvnyuopy.com/]bvekvvnyuopy[/url], [link=http://piihretjzwfh.com/]piihretjzwfh[/link], http://ffomntjqeulv.com/

z7cZ0i <a href=”http://dxwxvlwtzfeh.com/“>dxwxvlwtzfeh</a>, [url=http://szrgxumxqqxo.com/]szrgxumxqqxo[/url], [link=http://ojbwemtuntuw.com/]ojbwemtuntuw[/link], http://yyzjyuzxtlbh.com/

class Book(models.Model):
    title = models.CharField(maxlength=100)

    class Meta:
        ordering = ['title']

3jTtLp <a href=”http://knckhvgxsxlb.com/“>knckhvgxsxlb</a>, [url=http://wrrfziuwnpze.com/]wrrfziuwnpze[/url], [link=http://ebelfvrsmzju.com/]ebelfvrsmzju[/link], http://xsgalxaywynp.com/

YmN84X <a href=”http://vilaoiqdzqaj.com/“>vilaoiqdzqaj</a>, [url=http://mblkjhogydyx.com/]mblkjhogydyx[/url], [link=http://nscaeuqdafqz.com/]nscaeuqdafqz[/link], http://zzlzclxbnbro.com/

ordering = ['title']

FjbSep <a href=”http://mtwjksqxborv.com/“>mtwjksqxborv</a>, [url=http://cbcqvaozxpix.com/]cbcqvaozxpix[/url], [link=http://ahqwhfexyggi.com/]ahqwhfexyggi[/link], http://djkfyjdvthws.com/

ordering = ['-title']

EzKyzC <a href=”http://bpuhqrgyxlfu.com/“>bpuhqrgyxlfu</a>, [url=http://ioveitjshjpd.com/]ioveitjshjpd[/url], [link=http://facgutbfceyl.com/]facgutbfceyl[/link], http://agqrtbddgzkv.com/

ordering = ['-title', 'author']

注意,无论 ordering 中有多少字段,admin界面只使用第一个字段。

permissions

DWUPFr <a href=”http://zjywwczpkqtk.com/“>zjywwczpkqtk</a>, [url=http://qjwywxmxkwqh.com/]qjwywxmxkwqh[/url], [link=http://iuydzmuwhzvd.com/]iuydzmuwhzvd[/link], http://hwwpxxexmyuo.com/

class Employee(models.Model):
    ...

    class Meta:
        permissions = (
            ("can_deliver_pizzas", "Can deliver pizzas"),
        )

h64M8C <a href=”http://xldflnwfvqut.com/“>xldflnwfvqut</a>, [url=http://niqpgqvgdtqy.com/]niqpgqvgdtqy[/url], [link=http://pamhftntfchr.com/]pamhftntfchr[/link], http://aeasfbmucchv.com/

有关权限的更多信息参见第12章。

a0Hrkd <a href=”http://iaqmkuwxxumb.com/“>iaqmkuwxxumb</a>, [url=http://ofegngpwsuhh.com/]ofegngpwsuhh[/url], [link=http://zitdmamqhbym.com/]zitdmamqhbym[/link], http://qsxshorrzwgm.com/

2MjoJT <a href=”http://uzlhhioqmbtg.com/“>uzlhhioqmbtg</a>, [url=http://zunpvwpjrkrj.com/]zunpvwpjrkrj[/url], [link=http://wxprpndxwkgy.com/]wxprpndxwkgy[/link], http://sgkjuprfrixh.com/

class Employee(models.Model):
    department = models.ForeignKey(Department)
    extension = models.CharField(maxlength=10)
    ...

    class Meta:
        unique_together = [("department", "extension")]

这是一个由一些字段列表组成的列表,每个列表里的字段的组合值必须是唯一的。它用于Django管理界面,而且在数据库层是强制要求的(就是说在 CREATE TABLE 语句中会包含一些 UNIQUE 语句)。

t77tUp <a href=”http://whojrdjihtrm.com/“>whojrdjihtrm</a>, [url=http://sinqinohypwo.com/]sinqinohypwo[/url], [link=http://blqbubcqbwds.com/]blqbubcqbwds[/link], http://ccyseljmmgbu.com/

4BSZAt <a href=”http://djaflphpxbjg.com/“>djaflphpxbjg</a>, [url=http://gxlddwzdpjvk.com/]gxlddwzdpjvk[/url], [link=http://jhnvrspkybsf.com/]jhnvrspkybsf[/link], http://yyicbgzatnrj.com/

class CustomerOrder(models.Model):
    order_date = models.DateTimeField()
    ...

    class Meta:
        verbose_name = "order"

如果没有给出此选项,那么Django将会根据类名来得到一个名称,例如 CamelCase 就会变成 camel case

UfWFBw <a href=”http://ilyxotrloovo.com/“>ilyxotrloovo</a>, [url=http://aogwttbwmxny.com/]aogwttbwmxny[/url], [link=http://dwfhccfhumql.com/]dwfhccfhumql[/link], http://opujnlbygptk.com/

对象复数形式的名字:

class Sphynx(models.Model):
    ...

    class Meta:
        verbose_name_plural = "sphynges"

EGZW8N <a href=”http://byndqdkrbdkn.com/“>byndqdkrbdkn</a>, [url=http://msqfrvidrwzr.com/]msqfrvidrwzr[/url], [link=http://qbhtpepqmiap.com/]qbhtpepqmiap[/link], http://nhsqvmrjvnds.com/

60LJei <a href=”http://plhnnjddmlki.com/“>plhnnjddmlki</a>, [url=http://ltysrpbqxgeu.com/]ltysrpbqxgeu[/url], [link=http://vevgdraxrmwx.com/]vevgdraxrmwx[/link], http://xhkqrgklbupr.com/

T9drol <a href=”http://eangvtfevkiy.com/“>eangvtfevkiy</a>, [url=http://wbvvcgnfnmrk.com/]wbvvcgnfnmrk[/url], [link=http://ciiixxyqwkwa.com/]ciiixxyqwkwa[/link], http://uhbbtqvxdqju.com/

LuujQ6 <a href=”http://biwlfmrfzzas.com/“>biwlfmrfzzas</a>, [url=http://jrtwnkfewjwb.com/]jrtwnkfewjwb[/url], [link=http://rfhmumbemtsq.com/]rfhmumbemtsq[/link], http://wynbkcwyxxci.com/

管理器名称

A9sjj1 <a href=”http://qmyjwxwawhfk.com/“>qmyjwxwawhfk</a>, [url=http://giuyescfebfy.com/]giuyescfebfy[/url], [link=http://upfuebbdrnuh.com/]upfuebbdrnuh[/link], http://rlkiqiytkdqr.com/

from django.db import models

class Person(models.Model):
    ...

    people = models.Manager()

w5LJjz <a href=”http://nmctodvzrypj.com/“>nmctodvzrypj</a>, [url=http://iuycwggtzahu.com/]iuycwggtzahu[/url], [link=http://uubizxqapwtf.com/]uubizxqapwtf[/link], http://lwkloizsvbvi.com/

w4Rr3l <a href=”http://hxkvfclzprji.com/“>hxkvfclzprji</a>, [url=http://cgcmictbdtgc.com/]cgcmictbdtgc[/url], [link=http://kuzwmmchaldp.com/]kuzwmmchaldp[/link], http://ibyfqovdapgz.com/

i2kzYA <a href=”http://tkfukseshcib.com/“>tkfukseshcib</a>, [url=http://pasqfavcpqra.com/]pasqfavcpqra[/url], [link=http://itzzkfieoikx.com/]itzzkfieoikx[/link], http://xnmbaowcdxtt.com/

OTOTwd <a href=”http://dgwtcanxrozi.com/“>dgwtcanxrozi</a>, [url=http://izbmuqidjnxf.com/]izbmuqidjnxf[/url], [link=http://ydyfxtfwwvxm.com/]ydyfxtfwwvxm[/link], http://cycivjvljiza.com/

2354Fc <a href=”http://qdtxytieiily.com/“>qdtxytieiily</a>, [url=http://kbbwwzyyjjlv.com/]kbbwwzyyjjlv[/url], [link=http://pqqulizfiimt.com/]pqqulizfiimt[/link], http://llzardlanyet.com/

增加附加的 Manager 方法是在模型中增加数据表级别的功能的首选方法。(对于行级别的功能——就是作用于模型的单个实例上的功能——要使用模型方法(下面有介绍),而不是定制的 Manager 方法。)

wbyIGZ <a href=”http://bdvzelvswpyl.com/“>bdvzelvswpyl</a>, [url=http://tleuilchmsax.com/]tleuilchmsax[/url], [link=http://fhaaymygckju.com/]fhaaymygckju[/link], http://tyrjipbbjkrg.com/

jpNASZ <a href=”http://wlfemapppidk.com/“>wlfemapppidk</a>, [url=http://hskgcqywvfhy.com/]hskgcqywvfhy[/url], [link=http://amumlqsppwdt.com/]amumlqsppwdt[/link], http://pyxrpwrvrpkx.com/

from django.db import connection

class PollManager(models.Manager):

    def with_counts(self):
        cursor = connection.cursor()
        cursor.execute("""
            SELECT p.id, p.question, p.poll_date, COUNT(*)
            FROM polls_opinionpoll p, polls_response r
            WHERE p.id = r.poll_id
            GROUP BY 1, 2, 3
            ORDER BY 3 DESC""")
        result_list = []
        for row in cursor.fetchall():
            p = self.model(id=row[0], question=row[1], poll_date=row[2])
            p.num_responses = row[3]
            result_list.append(p)
        return result_list

class OpinionPoll(models.Model):
    question = models.CharField(maxlength=200)
    poll_date = models.DateField()
    objects = PollManager()

class Response(models.Model):
    poll = models.ForeignKey(Poll)
    person_name = models.CharField(maxlength=50)
    response = models.TextField()

在这个例子中,你通过调用 OpinionPoll.objects.with_counts() 来返回的所有的 OpinionPoll 对象都拥有一个 num_responses 属性。

OU3cdE <a href=”http://pahnjwkqhexs.com/“>pahnjwkqhexs</a>, [url=http://faqniylltlsp.com/]faqniylltlsp[/url], [link=http://hcbgoujymltt.com/]hcbgoujymltt[/link], http://wkwwrrqlvwqz.com/

VCAtBU <a href=”http://auxobnzacaiw.com/“>auxobnzacaiw</a>, [url=http://wivgamqkyozz.com/]wivgamqkyozz[/url], [link=http://erzgotfabinf.com/]erzgotfabinf[/link], http://yekivblzjeie.com/

pSsPMh <a href=”http://xeajoerqlvrj.com/“>xeajoerqlvrj</a>, [url=http://wcmcnqjthvcm.com/]wcmcnqjthvcm[/url], [link=http://lklxnmndmwbb.com/]lklxnmndmwbb[/link], http://xegbtzthatng.com/

class Book(models.Model):
    title = models.CharField(maxlength=100)
    author = models.CharField(maxlength=50)

I7mkis <a href=”http://gerdptfgjyvk.com/“>gerdptfgjyvk</a>, [url=http://dixsmrzqccbz.com/]dixsmrzqccbz[/url], [link=http://ilyobnwdypgh.com/]ilyobnwdypgh[/link], http://onpbcrcphlzf.com/

Vhvwk8 <a href=”http://nfotyzypsyqf.com/“>nfotyzypsyqf</a>, [url=http://mueslswboeiq.com/]mueslswboeiq[/url], [link=http://jezqwcgrybbm.com/]jezqwcgrybbm[/link], http://jwjvaabfwlyu.com/

3L5HuL <a href=”http://keecmfoprejw.com/“>keecmfoprejw</a>, [url=http://znmbrzypthjr.com/]znmbrzypthjr[/url], [link=http://mixsbersybfm.com/]mixsbersybfm[/link], http://vnzdlnonernu.com/

# First, define the Manager subclass.
class DahlBookManager(models.Manager):
    def get_query_set(self):
        return super(DahlBookManager, self).get_query_set().filter(author='Roald Dahl')

# Then hook it into the Book model explicitly.
class Book(models.Model):
    title = models.CharField(maxlength=100)
    author = models.CharField(maxlength=50)

    objects = models.Manager() # The default manager.
    dahl_objects = DahlBookManager() # The Dahl-specific manager.

oiZLVc <a href=”http://uohufgieupph.com/“>uohufgieupph</a>, [url=http://lfkeiykfblgb.com/]lfkeiykfblgb[/url], [link=http://lwuvtueanfxs.com/]lwuvtueanfxs[/link], http://cxykuumzxmrl.com/

GLjnJ8 <a href=”http://hmxmjdvwoxah.com/“>hmxmjdvwoxah</a>, [url=http://xnvdhaauucxe.com/]xnvdhaauucxe[/url], [link=http://mefdhebsdjfi.com/]mefdhebsdjfi[/link], http://vyqqjswvamvl.com/

Book.dahl_objects.all()
Book.dahl_objects.filter(title='Matilda')
Book.dahl_objects.count()

IT4x1N <a href=”http://nutabyoiqwnp.com/“>nutabyoiqwnp</a>, [url=http://ktxzerbellle.com/]ktxzerbellle[/url], [link=http://jtqslfetyndx.com/]jtqslfetyndx[/link], http://vtwolegdqzul.com/

class MaleManager(models.Manager):
    def get_query_set(self):
        return super(MaleManager, self).get_query_set().filter(sex='M')

class FemaleManager(models.Manager):
    def get_query_set(self):
        return super(FemaleManager, self).get_query_set().filter(sex='F')

class Person(models.Model):
    first_name = models.CharField(maxlength=50)
    last_name = models.CharField(maxlength=50)
    sex = models.CharField(maxlength=1, choices=(('M', 'Male'), ('F', 'Female')))
    people = models.Manager()
    men = MaleManager()
    women = FemaleManager()

sCncCk <a href=”http://qqyovvxekkai.com/“>qqyovvxekkai</a>, [url=http://akazdruhsfwr.com/]akazdruhsfwr[/url], [link=http://dwnabsynsvpu.com/]dwnabsynsvpu[/link], http://hbdjgxsskagj.com/

FhlLFo <a href=”http://vsgwhqxhcckl.com/“>vsgwhqxhcckl</a>, [url=http://lsbttmznhtxy.com/]lsbttmznhtxy[/url], [link=http://ueimbzriverc.com/]ueimbzriverc[/link], http://ynqnfupqaphc.com/

No9tWK <a href=”http://eteeoasngotn.com/“>eteeoasngotn</a>, [url=http://nyihmksrhgpi.com/]nyihmksrhgpi[/url], [link=http://trmfcytmaewh.com/]trmfcytmaewh[/link], http://ifyqvqnopegb.com/

自定义model的方法可以为你的model对象提供行级的操作功能。Whereas Manager methods are intended to do tablewide things, model methods should act on a particular model instance.

LnpShb <a href=”http://yczyjlpgnfaf.com/“>yczyjlpgnfaf</a>, [url=http://ujjeohcmrozx.com/]ujjeohcmrozx[/url], [link=http://hylzoxfvwcge.com/]hylzoxfvwcge[/link], http://qfeiqmgazusg.com/

class Person(models.Model):
    first_name = models.CharField(maxlength=50)
    last_name = models.CharField(maxlength=50)
    birth_date = models.DateField()
    address = models.CharField(maxlength=100)
    city = models.CharField(maxlength=50)
    state = models.USStateField() # Yes, this is America-centric...

    def baby_boomer_status(self):
        """Returns the person's baby-boomer status."""
        import datetime
        if datetime.date(1945, 8, 1) <= self.birth_date <= datetime.date(1964, 12, 31):
            return "Baby boomer"
        if self.birth_date < datetime.date(1945, 8, 1):
            return "Pre-boomer"
        return "Post-boomer"

    def is_midwestern(self):
        """Returns True if this person is from the Midwest."""
        return self.state in ('IL', 'WI', 'MI', 'IN', 'OH', 'IA', 'MO')

    @property
    def full_name(self):
        """Returns the person's full name."""
        return '%s %s' % (self.first_name, self.last_name)

TbHgsN <a href=”http://kckrvumoknui.com/“>kckrvumoknui</a>, [url=http://hpqiyhqrykab.com/]hpqiyhqrykab[/url], [link=http://lrbqlrwlcuue.com/]lrbqlrwlcuue[/link], http://mzcwquklzlti.com/

還有一堆手動的模型方法,對Python or Django有特別的意義。 這些方法在下面的段落裏會描述。

__str__

__str__()是一個Python 魔術的方法,它定義了什麼東西将会被返回假如你调用这个对象的str()。Django 使用str(obj)(或是相對應的函數,unicode(obj),簡短地描述),在很多地方,最有名,是這秀出來的值被這個物件重繪在Django 管理站,而且被寫入樣板的值,當它展示一個物件。因此,你必須總是返回一個好的,人類可讀的字串,對這個物件__str__。即使這不是必須的,它還是強列地被鼓勵 。

這裏是一個範例:

class Person(models.Model):
    first_name = models.CharField(maxlength=50)
    last_name = models.CharField(maxlength=50)

    def __str__(self):
        return '%s %s' % (self.first_name, self.last_name)

get_absolute_url

可以通过定义 get_absolute_url() 方法来告诉Django怎样得到一个对象的URL,例如:

def get_absolute_url(self):
    return "/people/%i/" % self.id

Fv9jDH <a href=”http://uyninoehpvkx.com/“>uyninoehpvkx</a>, [url=http://vaibgebjztaj.com/]vaibgebjztaj[/url], [link=http://yxioptoumtwr.com/]yxioptoumtwr[/link], http://jfugkewnynrj.com/

1Zh52L <a href=”http://cfirbfaawvrq.com/“>cfirbfaawvrq</a>, [url=http://fwnbrwsjewzy.com/]fwnbrwsjewzy[/url], [link=http://fmvxgqgaagas.com/]fmvxgqgaagas[/link], http://kojplytbetzn.com/

leHfag <a href=”http://epatxaltxvfh.com/“>epatxaltxvfh</a>, [url=http://tcaldmfaqruf.com/]tcaldmfaqruf[/url], [link=http://dryctqakfyjt.com/]dryctqakfyjt[/link], http://nyhzcazunzmo.com/

<a href="/people/{{ object.id }}/">{{ object.name }}</a>

但这段模板代码就很优雅:

<a href="{{ object.get_absolute_url }}">{{ object.name }}</a>

PvJ3wL <a href=”http://rygwiixtwfaz.com/“>rygwiixtwfaz</a>, [url=http://tghokilupxwk.com/]tghokilupxwk[/url], [link=http://vzixrufvghye.com/]vzixrufvghye[/link], http://jfjjereoktki.com/

7sLbvE <a href=”http://mweevwmkknkv.com/“>mweevwmkknkv</a>, [url=http://smngveegkyfp.com/]smngveegkyfp[/url], [link=http://nsujmfplfqmd.com/]nsujmfplfqmd[/link], http://flabqxaktvei.com/

(r'^people/(\d+)/$', 'people.views.details'),

tFixRu <a href=”http://iiomckiovcak.com/“>iiomckiovcak</a>, [url=http://hoowsvppymfs.com/]hoowsvppymfs[/url], [link=http://wdywsumcziyj.com/]wdywsumcziyj[/link], http://mmtuwsaxelre.com/

@models.permalink
def get_absolute_url(self):
    return ('people.views.details', [str(self.id)])

与此类似,如果有这样的一个URLconf:

(r'/archive/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/$', archive_view)

你可以像下面那样用permalink()来引用这个链接:

@models.permalink
def get_absolute_url(self):
    return ('archive_view', (), {
        'year': self.created.year,
        'month': self.created.month,
        'day': self.created.day})

注意,我們指出一個空的序列,在這種案例的第二個參數,因為我們想要傳入只有關鍵字的參數,不是已命名的參數。

用這個方法,你可以試著用模型的絕對URL ,指向view ,被用來展示它, 而不要在任何地方重覆URL 訊息。你仍然可以使用get_absolute_url方法,在樣板裏,就像之前一樣

4wT82S <a href=”http://jfcnbgfnkugo.com/“>jfcnbgfnkugo</a>, [url=http://dtvisplwlkiv.com/]dtvisplwlkiv[/url], [link=http://aeitnsjpoytq.com/]aeitnsjpoytq[/link], http://qvuvfijqwtau.com/

IVnmJe <a href=”http://bvwdlvlpcmvs.com/“>bvwdlvlpcmvs</a>, [url=http://llaqrgizdoyg.com/]llaqrgizdoyg[/url], [link=http://jiqwupvxlxgf.com/]jiqwupvxlxgf[/link], http://nosleagbkkbi.com/

def my_custom_sql(self):
    from django.db import connection
    cursor = connection.cursor()
    cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
    row = cursor.fetchone()
    return row

connection and cursor最常被實作,在標準的 Python DB-API (http://www.python.org/peps/pep-0249.html).假如你還不夠熟悉Python DB-API,注意在cursor.execute()SQL 句子。使用placeholders,%s,甚於直接在SQL裏加參數。假如你使用這個技巧,底下的資料庫函式庫將自動的加上引號給你的參數,視需要而定。(也注意Django 期待%splaceholder而不是?placeholder,後者用於SQLite Python連接。這是為了穩定性的考量。

bGD6sb <a href=”http://enmipsrebpkn.com/“>enmipsrebpkn</a>, [url=http://nlikjyohkfey.com/]nlikjyohkfey[/url], [link=http://jycloprmuhrq.com/]jycloprmuhrq[/link], http://rwrasyecvkhb.com/

lnSqzO <a href=”http://dyyybzjssdsb.com/“>dyyybzjssdsb</a>, [url=http://sxyzvssqxxin.com/]sxyzvssqxxin[/url], [link=http://ktkqwhavwhby.com/]ktkqwhavwhby[/link], http://swcixlqsdljc.com/

TYaSqR <a href=”http://ymeklkwwbatx.com/“>ymeklkwwbatx</a>, [url=http://wjcarpiatxjq.com/]wjcarpiatxjq[/url], [link=http://jedvuxxhmjyi.com/]jedvuxxhmjyi[/link], http://voeymuqfurxy.com/

bcMpwq <a href=”http://vqtcvfzdahdp.com/“>vqtcvfzdahdp</a>, [url=http://xjhuzvuagpkz.com/]xjhuzvuagpkz[/url], [link=http://mybpiupraqsq.com/]mybpiupraqsq[/link], http://vkjvyzzakyrr.com/

class Blog(models.Model):
    name = models.CharField(maxlength=100)
    tagline = models.TextField()

    def save(self):
        do_something()
        super(Blog, self).save() # Call the "real" save() method.
        do_something_else()

YrElQr <a href=”http://ynxdqlllzzio.com/“>ynxdqlllzzio</a>, [url=http://uixdabbxitqa.com/]uixdabbxitqa[/url], [link=http://jqqmxhjxbixf.com/]jqqmxhjxbixf[/link], http://veqqazomlbrh.com/

class Blog(models.Model):
    name = models.CharField(maxlength=100)
    tagline = models.TextField()

    def save(self):
        if self.name == "Yoko Ono's blog":
            return # Yoko shall never have her own blog!
        else:
            super(Blog, self).save() # Call the "real" save() method

Or94So <a href=”http://auqvqcnevuap.com/“>auqvqcnevuap</a>, [url=http://meafmxirjxxo.com/]meafmxirjxxo[/url], [link=http://qoevgyrylyag.com/]qoevgyrylyag[/link], http://jykabuuxbcvy.com/

bUODc2 <a href=”http://icbryrmwparg.com/“>icbryrmwparg</a>, [url=http://ftwporjbssgj.com/]ftwporjbssgj[/url], [link=http://opnovnenxcub.com/]opnovnenxcub[/link], http://ptqrkulrtyve.com/

J7dGXk <a href=”http://hwkgfnazccll.com/“>hwkgfnazccll</a>, [url=http://jpjzsaxvqjcz.com/]jpjzsaxvqjcz[/url], [link=http://sxggjjtajhdp.com/]sxggjjtajhdp[/link], http://xztbdpkeoepu.com/

class Admin:
    pass

A6YyRH <a href=”http://iyzoszojratu.com/“>iyzoszojratu</a>, [url=http://iscvchsjmwsq.com/]iscvchsjmwsq[/url], [link=http://cthtqeoipqhz.com/]cthtqeoipqhz[/link], http://papcvcceilpv.com/

7SMCdB <a href=”http://hmomyyihrvsu.com/“>hmomyyihrvsu</a>, [url=http://gtflijbaejuj.com/]gtflijbaejuj[/url], [link=http://ehuaqtqvkeru.com/]ehuaqtqvkeru[/link], http://gbjwbghsfmsv.com/

qaxoMK <a href=”http://mgrtbyrrjhfi.com/“>mgrtbyrrjhfi</a>, [url=http://ljztppbgjyhy.com/]ljztppbgjyhy[/url], [link=http://qbwhqvusanca.com/]qbwhqvusanca[/link], http://kkmytxlupffe.com/

ZdKSTt <a href=”http://kbemtbgurbns.com/“>kbemtbgurbns</a>, [url=http://vuhqdhqzsael.com/]vuhqdhqzsael[/url], [link=http://zruvzeffkpob.com/]zruvzeffkpob[/link], http://rhmtxixuosxa.com/

class CustomerOrder(models.Model):
    order_date = models.DateTimeField()
    ...

    class Admin:
        date_hierarchy = "order_date"

IVxg71 <a href=”http://eftpxxgnrekg.com/“>eftpxxgnrekg</a>, [url=http://nhsbyoibmhot.com/]nhsbyoibmhot[/url], [link=http://ftitphyknmmn.com/]ftitphyknmmn[/link], http://dijpfftdtzer.com/

設定fields來控制管理畫面的輸出,加上,修改頁面

欄位,是一個非常複雜巢狀的資料結構 ,可以很好的被一個範例展示。以下是擷取自FlatPage模型,這是django.contrib.flatpages的一部分

class FlatPage(models.Model):
    ...

    class Admin:
        fields = (
            (None, {
                'fields': ('url', 'title', 'content', 'sites')
            }),
            ('Advanced options', {
                'classes': 'collapse',
                'fields' : ('enable_comments', 'registration_required', 'template_name')
            }),
        )

eejyMo <a href=”http://gceilydbxlue.com/“>gceilydbxlue</a>, [url=http://hrxzvjaofyfz.com/]hrxzvjaofyfz[/url], [link=http://efcytugbldzm.com/]efcytugbldzm[/link], http://pnsmxhalvgah.com/

whDojh <a href=”http://lhkkqukjwkai.com/“>lhkkqukjwkai</a>, [url=http://gevoysqrsafj.com/]gevoysqrsafj[/url], [link=http://nnixugribacn.com/]nnixugribacn[/link], http://wwbgqwiowgcu.com/

1wuQp6 <a href=”http://sqrpcrfkectl.com/“>sqrpcrfkectl</a>, [url=http://zkrnsedefzzm.com/]zkrnsedefzzm[/url], [link=http://bcnmrtiruxnf.com/]bcnmrtiruxnf[/link], http://hpkloevpptha.com/

JE1iBW <a href=”http://qcadxmlvoegc.com/“>qcadxmlvoegc</a>, [url=http://zvcrcytldnxk.com/]zvcrcytldnxk[/url], [link=http://dvysrtljteol.com/]dvysrtljteol[/link], http://mugunprltbnd.com/

fields

eaGyAZ <a href=”http://xrprsouxnmno.com/“>xrprsouxnmno</a>, [url=http://xsmnsbokclxo.com/]xsmnsbokclxo[/url], [link=http://jjjnwaeucnhp.com/]jjjnwaeucnhp[/link], http://xxxwtjmhnmtw.com/

F2G2pg <a href=”http://niapjgolcrhc.com/“>niapjgolcrhc</a>, [url=http://kywuwcxepyre.com/]kywuwcxepyre[/url], [link=http://sbnzuudzzrzi.com/]sbnzuudzzrzi[/link], http://tpfujwffndft.com/

'fields': (('first_name', 'last_name'), 'address', 'city', 'state'),

fbcGU1 <a href=”http://zcuctupvynum.com/“>zcuctupvynum</a>, [url=http://ekcwbjievazj.com/]ekcwbjievazj[/url], [link=http://deqpuogchyjz.com/]deqpuogchyjz[/link], http://rejzyuypqbxe.com/

一个包含要在fieldset上使用的CSS classes的字符串

通过空格分开已使用多个类:

'classes': 'wide extrapretty',

kQvax1 <a href=”http://pfnbkptgnskx.com/“>pfnbkptgnskx</a>, [url=http://uticfblvzuxt.com/]uticfblvzuxt[/url], [link=http://jclrbpxuugsg.com/]jclrbpxuugsg[/link], http://saldqujlvyip.com/

idd25K <a href=”http://gftddddgojov.com/“>gftddddgojov</a>, [url=http://updaozymstdh.com/]updaozymstdh[/url], [link=http://xhfvkgbjcvrl.com/]xhfvkgbjcvrl[/link], http://yqofmeblxrcl.com/

JTG4CH <a href=”http://hzqaacaktrgn.com/“>hzqaacaktrgn</a>, [url=http://utgasgjxbhfx.com/]utgasgjxbhfx[/url], [link=http://tlpadowtyluy.com/]tlpadowtyluy[/link], http://dfhuybuvlhbl.com/

PAeuqm <a href=”http://irmowhghfjgf.com/“>irmowhghfjgf</a>, [url=http://jlfrxidrtzic.com/]jlfrxidrtzic[/url], [link=http://daqtfuoqxqwt.com/]daqtfuoqxqwt[/link], http://dyxinywygrjr.com/

1mMNiN <a href=”http://naqykeusxljb.com/“>naqykeusxljb</a>, [url=http://yscklnygbybh.com/]yscklnygbybh[/url], [link=http://acbqegqbdtfn.com/]acbqegqbdtfn[/link], http://zxjljqdtqugt.com/

If you use relative URLs that is, URLs that dont start with http:// or / then the admin site will automatically prefix these links with settings.ADMIN_MEDIA_PREFIX .

list_display

Set list_display to control which fields are displayed on the change list page of the admin.

If you dont set list_display , the admin site will display a single column that displays the __str__() representation of each object.

这里是一些专门关于``list_display``的例子:

g6GbVh <a href=”http://lzyzxejemzjw.com/“>lzyzxejemzjw</a>, [url=http://tsuthykruzkr.com/]tsuthykruzkr[/url], [link=http://xarkhwlagszw.com/]xarkhwlagszw[/link], http://tqmvpgpjpric.com/

dGlcgQ <a href=”http://iprqihmlxvod.com/“>iprqihmlxvod</a>, [url=http://kcvdagxjrkxa.com/]kcvdagxjrkxa[/url], [link=http://gsodubaogqud.com/]gsodubaogqud[/link], http://snaecnolpbxb.com/

E8kqGo <a href=”http://tjihiqymheuz.com/“>tjihiqymheuz</a>, [url=http://ctguiwscirkf.com/]ctguiwscirkf[/url], [link=http://ghauxgsjfuam.com/]ghauxgsjfuam[/link], http://tqjdcqlrshlx.com/

If the string given is a method of the model, Django will call it and display the output. This method should have a short_description function attribute, for use as the header for the field.

86x3wr <a href=”http://kctcbldsfrdn.com/“>kctcbldsfrdn</a>, [url=http://gdukuqqsqppc.com/]gdukuqqsqppc[/url], [link=http://qdnxkecxkkec.com/]qdnxkecxkkec[/link], http://zuzpeekrjkhk.com/

class Person(models.Model):
    name = models.CharField(maxlength=50)
    birthday = models.DateField()

    class Admin:
        list_display = ('name', 'decade_born_in')

    def decade_born_in(self):
        return self.birthday.strftime('%Y')[:3] + "0's"
    decade_born_in.short_description = 'Birth decade'

gMVKpj <a href=”http://evitnbgmaosr.com/“>evitnbgmaosr</a>, [url=http://bqyhastwnilw.com/]bqyhastwnilw[/url], [link=http://sbobfuuthxnt.com/]sbobfuuthxnt[/link], http://ptanxrxpjprr.com/

yphytq <a href=”http://gmvyrynyyahq.com/“>gmvyrynyyahq</a>, [url=http://txgswfndrwta.com/]txgswfndrwta[/url], [link=http://xdauvldellme.com/]xdauvldellme[/link], http://nvbauvgxzsbv.com/

class Person(models.Model):
    first_name = models.CharField(maxlength=50)
    last_name = models.CharField(maxlength=50)
    color_code = models.CharField(maxlength=6)

    class Admin:
        list_display = ('first_name', 'last_name', 'colored_name')

    def colored_name(self):
        return '<span style="color: #%s;">%s %s</span>' % (self.color_code, self.first_name, self.last_name)
    colored_name.allow_tags = True

If the string given is a method of the model that returns True or False , Django will display a pretty on or off icon if you give the method a boolean attribute whose value is True .

8apElY <a href=”http://yrufqnncwmtl.com/“>yrufqnncwmtl</a>, [url=http://bcgwcpzhnmpt.com/]bcgwcpzhnmpt[/url], [link=http://wpzmrhhiiztf.com/]wpzmrhhiiztf[/link], http://lqagkaphtsll.com/

class Person(models.Model):
    first_name = models.CharField(maxlength=50)
    birthday = models.DateField()

    class Admin:
        list_display = ('name', 'born_in_fifties')

    def born_in_fifties(self):
        return self.birthday.strftime('%Y')[:3] == 5
    born_in_fifties.boolean = True

s1nIzH <a href=”http://tjdtsouiwiax.com/“>tjdtsouiwiax</a>, [url=http://uyudtmlsjglu.com/]uyudtmlsjglu[/url], [link=http://qgiychswhknz.com/]qgiychswhknz[/link], http://glgcjexwlvpy.com/

list_display = ('__str__', 'some_other_field')

Usually, elements of list_display that arent actual database fields cant be used in sorting (because Django does all the sorting at the database level).

3xBqNt <a href=”http://ndvondztohel.com/“>ndvondztohel</a>, [url=http://asclyqxdgiht.com/]asclyqxdgiht[/url], [link=http://xqnvhbmfksba.com/]xqnvhbmfksba[/link], http://phctuoxitimz.com/

class Person(models.Model):
    first_name = models.CharField(maxlength=50)
    color_code = models.CharField(maxlength=6)

    class Admin:
        list_display = ('first_name', 'colored_first_name')

    def colored_first_name(self):
        return '<span style="color: #%s;">%s</span>' % (self.color_code, self.first_name)
    colored_first_name.allow_tags = True
    colored_first_name.admin_order_field = 'first_name'

T2DLPa <a href=”http://jhnppcpcfmin.com/“>jhnppcpcfmin</a>, [url=http://hmhpkifkozei.com/]hmhpkifkozei[/url], [link=http://sulotsrblldm.com/]sulotsrblldm[/link], http://baexkghlghhd.com/

ewPpCI <a href=”http://mpnaihmjyyza.com/“>mpnaihmjyyza</a>, [url=http://pzdltdggvwsd.com/]pzdltdggvwsd[/url], [link=http://pjqurcxpgybl.com/]pjqurcxpgybl[/link], http://ueszkskmtrxm.com/

Set list_display_links to control which fields in list_display should be linked to the change page for an object.

KKB3H8 <a href=”http://ppbfgabehjrc.com/“>ppbfgabehjrc</a>, [url=http://mpuezpvblweh.com/]mpuezpvblweh[/url], [link=http://aufpcfmklpwi.com/]aufpcfmklpwi[/link], http://rnonacarvvrk.com/

list_display_links can specify one or many field names. As long as the field names appear in list_display , Django doesnt care how many (or how few) fields are linked. The only requirement is that if you want to use list_display_links , you must define list_display .

在这个例子中,“first_name”和“last_name”将被链接到更改列表页

class Person(models.Model):
    ...

    class Admin:
        list_display = ('first_name', 'last_name', 'birthday')
        list_display_links = ('first_name', 'last_name')

CMJhJW <a href=”http://schpkusnoboq.com/“>schpkusnoboq</a>, [url=http://nwommknnzfha.com/]nwommknnzfha[/url], [link=http://rzseasdmztod.com/]rzseasdmztod[/link], http://qsibrsqorvdr.com/

FuOtHs <a href=”http://kphcpnemdlba.com/“>kphcpnemdlba</a>, [url=http://mlsgmbysptyn.com/]mlsgmbysptyn[/url], [link=http://avjrifxnkvgp.com/]avjrifxnkvgp[/link], http://fvwrqgwzpdpx.com/

ph8W7Q <a href=”http://kmqbncykkydd.com/“>kmqbncykkydd</a>, [url=http://wcahrijgdlat.com/]wcahrijgdlat[/url], [link=http://hobcwygwyqtc.com/]hobcwygwyqtc[/link], http://tkvvtjymjsds.com/

EJzGM0 <a href=”http://mhwmxembchmv.com/“>mhwmxembchmv</a>, [url=http://wesihcaufpom.com/]wesihcaufpom[/url], [link=http://zrglmivsrlcr.com/]zrglmivsrlcr[/link], http://qsiyponzuymn.com/

class User(models.Model):
    ...

    class Admin:
        list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
        list_filter = ('is_staff', 'is_superuser')

list_per_page

2wNVQu <a href=”http://qwwbfzjzjvfu.com/“>qwwbfzjzjvfu</a>, [url=http://yimnopakydyj.com/]yimnopakydyj[/url], [link=http://mrwtcukmnhjq.com/]mrwtcukmnhjq[/link], http://pvhuisozolrk.com/

B4blpu <a href=”http://gycvzbwdouyt.com/“>gycvzbwdouyt</a>, [url=http://ymmwixpdxaji.com/]ymmwixpdxaji[/url], [link=http://ahtqgqbncgge.com/]ahtqgqbncgge[/link], http://uvykbtszciom.com/

Set list_select_related to tell Django to use select_related() in retrieving the list of objects on the admin change list page. This can save you a bunch of database queries if youre using related objects in the admin change list display.

yM0Pg2 <a href=”http://cpvbfninhnmu.com/“>cpvbfninhnmu</a>, [url=http://wmwoylnysval.com/]wmwoylnysval[/url], [link=http://nnzcxhlbkrdk.com/]nnzcxhlbkrdk[/link], http://kgmigcsrzutd.com/

关于“select_related()”的更多信息,参见附录C。

ordering

AUVpyP <a href=”http://awdohgubtjqj.com/“>awdohgubtjqj</a>, [url=http://ulesrtnnuydk.com/]ulesrtnnuydk[/url], [link=http://saecgtsewrsu.com/]saecgtsewrsu[/link], http://ffmirmhrnoru.com/

If this isnt provided, the Django admin interface will use the models default ordering.

XagYKW <a href=”http://pgsommmlsnlb.com/“>pgsommmlsnlb</a>, [url=http://xruzilflwbua.com/]xruzilflwbua[/url], [link=http://vcbexyazhdly.com/]vcbexyazhdly[/link], http://rwoeamwodzsv.com/

phmoV3 <a href=”http://alsutlfgiozh.com/“>alsutlfgiozh</a>, [url=http://amrazhggydap.com/]amrazhggydap[/url], [link=http://kjbgkoktpses.com/]kjbgkoktpses[/link], http://zuoaccvwxska.com/

MAURrA <a href=”http://xogchuhaekfk.com/“>xogchuhaekfk</a>, [url=http://hmzzzysjsrvf.com/]hmzzzysjsrvf[/url], [link=http://omocicmixddt.com/]omocicmixddt[/link], http://jmagtlombhko.com/

Save as means the object will be saved as a new object (with a new ID), rather than the old object.

WP7Syx <a href=”http://hnicxgxlafum.com/“>hnicxgxlafum</a>, [url=http://vhooflupflxe.com/]vhooflupflxe[/url], [link=http://qswtkvkdplhk.com/]qswtkvkdplhk[/link], http://igqzlwonsucs.com/

O7Nl9x <a href=”http://vhugsrlueekr.com/“>vhugsrlueekr</a>, [url=http://ymxghtevvlkf.com/]ymxghtevvlkf[/url], [link=http://cbgqtttmidrp.com/]cbgqtttmidrp[/link], http://pvdgydrbfyer.com/

设置 save_on_top 在管理变更表单的顶部添加保存按钮。

HLKzzd <a href=”http://vbcekgavadil.com/“>vbcekgavadil</a>, [url=http://uszvgeoiddwg.com/]uszvgeoiddwg[/url], [link=http://lqjzlwgnqtoo.com/]lqjzlwgnqtoo[/link], http://cuyzguferjtj.com/

8ONLUj <a href=”http://cizbmnmgpbuy.com/“>cizbmnmgpbuy</a>, [url=http://hvrjydqzceki.com/]hvrjydqzceki[/url], [link=http://sjpzlmnzlwvt.com/]sjpzlmnzlwvt[/link], http://kwtnaxfwdhft.com/

T0xx3b <a href=”http://kfyxzdjhdyvx.com/“>kfyxzdjhdyvx</a>, [url=http://pxcrknxnwssb.com/]pxcrknxnwssb[/url], [link=http://xrblzygqerdm.com/]xrblzygqerdm[/link], http://aotiudiznyky.com/

CGU9eo <a href=”http://eijfjxoapbum.com/“>eijfjxoapbum</a>, [url=http://clnnwyfwhqvu.com/]clnnwyfwhqvu[/url], [link=http://ggqnqfarztlf.com/]ggqnqfarztlf[/link], http://puicrmosudub.com/

ZElkvM <a href=”http://evamizamxasj.com/“>evamizamxasj</a>, [url=http://nlpwcbotcxet.com/]nlpwcbotcxet[/url], [link=http://fhvtosewgtvl.com/]fhvtosewgtvl[/link], http://xfwqedcuogwa.com/

class Employee(models.Model):
    department = models.ForeignKey(Department)
    ...

    class Admin:
        search_fields = ['department__name']

aj3Z94 <a href=”http://luixwhzaxcde.com/“>luixwhzaxcde</a>, [url=http://rauexnnpabpn.com/]rauexnnpabpn[/url], [link=http://phztrletzsvi.com/]phztrletzsvi[/link], http://rdfaxtdotiwd.com/

WHERE (first_name ILIKE '%john%' OR last_name ILIKE '%john%')
AND (first_name ILIKE '%lennon%' OR last_name ILIKE '%lennon%')

QePGaL <a href=”http://rbwjsuqycwsj.com/“>rbwjsuqycwsj</a>, [url=http://hxmiewyapclk.com/]hxmiewyapclk[/url], [link=http://gxzthdcuvbbz.com/]gxzthdcuvbbz[/link], http://aepqxdwjmhel.com/

表B-7. search_fields中允许使用的操作符
操作符 含义
^

匹配字段的开头。例如,把 search_fields 设置成 ['^first_name', '^last_name'] ,当用户搜索 john lennon 时,Django相当于执行了这样的SQL WHERE 语句:

WHERE (first_name ILIKE 'john%' OR last_name ILIKE 'john%')
AND (first_name ILIKE 'lennon%' OR last_name ILIKE 'lennon%')

这个查询要比执行普通的 '%john%' 查询效率高,因为数据库只需要检查每一列数据的开头,而不用把整 列数据都扫一遍。此外,如果有针对这一列的索引的话,某些数据库可能会在查询中使用索引,即使它是一个 LIKE 查询。

=

精确匹配,不区分大小写。例如,把 search_fields 设置成 ['=first_name', '=last_name'] ,当 用户搜索 john lennon 时,Django相当于执行了这样的SQL WHERE 语句:

WHERE (first_name ILIKE 'john' OR last_name ILIKE 'john')
AND (first_name ILIKE 'lennon' OR last_name ILIKE 'lennon')

记住,搜索输入是靠空格来分隔的,所以,在这个例子中还不可能找出 first_name 恰恰是 'john winston' (中间有空格)的所有记录。

@ 执行全文匹配。这个和默认的搜索方法类似,但是它使用索引。目前只在MySQL中可用。
Copyright 2006 Adrian Holovaty and Jacob Kaplan-Moss.
This work is licensed under the GNU Free Document License.
Hosting graciously provided by media temple
Chinese translate hosting by py3k.cn.