在使用django自带的admin后台的时候,他提供了一些默认的指令可以对数据进行操作, 比如批量删除,修改等 同样的我们也可以添加自己的指令。
|- django版本:1.8
|- python版本:3.4
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
class story(models.model):
#编辑story状态
status_choices = (
( 1 , '待编辑状态' ),
( 2 , '待批准状态' ),
( 3 , '已发布状态' ),
( 4 , '已存档' ),
)
title = models.charfield(max_length = 100 , verbose_name = u '文章' )
slug = models.slugfield(verbose_name = u '简短标题' )
category = models.foreignkey(category, verbose_name = u '分类' )
owner = models.foreignkey(user, verbose_name = u '发布者' )
status = models.integerfield(choices = status_choices, default = 1 , verbose_name = u '发布状态' )
class meta:
ordering = [ 'modified' , 'created' ]
verbose_name_plural = verbose_name = '文章'
def __str__( self ):
return self .title
|
admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class storyadmin(admin.modeladmin):
list_display = ( 'title' , 'owner' , 'status' , 'created' , 'modified' , 'read_times' , 'top' , 'id' )
search_fields = ( 'title' , 'markdown_content' )
# 状态选择器
list_filter = ( 'status' , 'owner' , 'created' , 'modified' ,)
prepopulated_fields = { 'slug' : ( 'title' ,)}
actions = [ 'make_story_public' ]
def make_story_public( self , request, queryset):
# queryset参数为选中的story对象
rows_updated = queryset.update(status = 3 )
message_bit = "%s 篇文章" % rows_updated
self .message_user(request, "%s 已成功标记为已发布状态." % message_bit)
make_story_public.short_description = u '修改选中文章为已发布状态'
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/Lands-ljk/p/5589157.html