python学习笔记--Django入门四 管理站点--二

接上一节  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 NULLide

可是,其它数据类型有例外:日期型、时间型和数字型字段不接受空字符串。post

在这种状况下,NULL是惟一指定空值的方法。 在Django模块中,你能够经过添加null=True来指定一个字段容许为NULL学习

若是你想容许一个日期型(DateFieldTimeFieldDateTimeField)或数字型(IntegerFieldDecimalFieldFloatField)字段为空,你须要使用null=True * 和* blank=Truethis

为了举例说明,让咱们把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. 

Customizing Field Labels 格式化标签

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

Customizing Change Lists and Forms

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.

相关文章
相关标签/搜索