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

接上一节  python学习笔记--Django入门四 管理站点

设置字段可选

编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下:

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和空字符串查找么?还是仅按字符串查找?

为了消除歧义,Django生成CREATE TABLE语句自动为每个字段显式加上NOT NULL

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

在这种情况下,NULL是唯一指定空值的方法。 在Django模块中,你可以通过添加null=True来指定一个字段允许为NULL

如果你想允许一个日期型(DateFieldTimeFieldDateTimeField)或数字型(IntegerFieldDecimalFieldFloatField)字段为空,你需要使用null=True * 和* blank=True

为了举例说明,让我们把Book模块修改成允许 publication_date为空。修改后的代码如下:

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编辑页面中看到这个新标签。

python学习笔记--Django入门四 管理站点--二_第1张图片

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 

python学习笔记--Django入门四 管理站点--二_第2张图片

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. 

python学习笔记--Django入门四 管理站点--二_第3张图片

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.

python学习笔记--Django入门四 管理站点--二_第4张图片

 

转载于:https://www.cnblogs.com/taiguyiba/p/6064826.html

你可能感兴趣的:(python,数据库)