接上一節 python學習筆記--Django入門四 管理站點html
編輯Book模塊在email字段上加上blank=True,指定email字段爲可選,代碼以下:python
class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) email = models.EmailField(blank=True )
全部字段都默認blank=False,這使得它們不容許輸入空值。刷新頁面可用。數據庫
在SQL中, NULL的值不一樣於空字符串,就像Python中None不一樣於空字符串("")同樣。這意味着某個字符型字段(如VARCHAR)的值不可能同時包含NULL和空字符串。服務器
這會引發沒必要要的歧義或疑惑。 爲何這條記錄有個NULL,而那條記錄卻有個空字符串? 它們之間有區別,仍是數據輸入不一致? 還有: 我怎樣才能獲得所有擁有空值的記錄,應該按NULL和空字符串查找麼?仍是僅按字符串查找?app
爲了消除歧義,Django生成CREATE TABLE語句自動爲每一個字段顯式加上NOT NULL。ide
可是,其它數據類型有例外:日期型、時間型和數字型字段不接受空字符串。post
在這種狀況下,NULL是惟一指定空值的方法。 在Django模塊中,你能夠經過添加null=True來指定一個字段容許爲NULL。學習
若是你想容許一個日期型(DateField、TimeField、DateTimeField)或數字型(IntegerField、DecimalField、FloatField)字段爲空,你須要使用null=True * 和* blank=True。this
爲了舉例說明,讓咱們把Book模塊修改爲容許 publication_date爲空。修改後的代碼以下:spa
class Book(models.Model): title = models.CharField(max_length=100) authors = models.ManyToManyField(Author) publisher = models.ForeignKey(Publisher) publication_date = models.DateField(blank=True, null=True)
添加null=True比添加blank=True複雜。由於null=True改變了數據的語義,即改變了CREATE TABLE語句,把publication_date字段上的NOT NULL刪除了。 要完成這些改動,咱們還須要更新數據庫。
ALTER TABLE books_book ALTER COLUMN publication_date DROP NOT NULL;
# MySQL寫法:
alter table books_book modify column publication_date DATE default null;
修改後重啓服務器,你會在author編輯頁面中看到這個新標籤。
Django does not attempt to automate changes to database schemas, so it’s your own responsibility to execute the python manage.py migrate
command whenever you make such a change to a model.
change the label of the Author.email
field to 「e-mail,」 with a hyphen:
class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) email = models.EmailField(blank=True, verbose_name ='e-mail')
class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) email = models.EmailField(blank=True, verbose_name ='e-mail') def __str__(self): return u'%s %s' % (self.first_name, self.last_name)
As a result, the change list for Author
objects displays each other’s first name and last name together, as
to see each author’s e-mail address in this list, and it’d be nice to be able to sort by first and last name. To make this happen, we’ll define a ModelAdmin
class for the Author
model.
date filters
class BookAdmin(admin.ModelAdmin): list_display = ('title', 'publisher', 'publication_date') list_filter = ('publication_date',) admin.site.register(Publisher) admin.site.register(Author, AuthorAdmin) admin.site.register(Book, BookAdmin)
First, we defined a list_display
just to make the change list look a bit nicer. Then, we used list_filter
, which is set to a tuple of fields to use to create filters along the right side of the change list page.
date_hierarchy
class BookAdmin(admin.ModelAdmin): list_display = ('title', 'publisher','publication_date') list_filter = ('publication_date',) date_hierarchy = 'publication_date'
the change list page gets a date drill-down navigation bar at the top of the list
ordering
class BookAdmin(admin.ModelAdmin): list_display = ('title', 'publisher','publication_date') list_filter = ('publication_date',) date_hierarchy = 'publication_date' ordering = ('-publication_date',)
Just pass a list or tuple of field names, and add a minus sign to a field to use descending sort order.