Django学习笔记:为Model添加Action
Posted Data&Truth
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django学习笔记:为Model添加Action相关的知识,希望对你有一定的参考价值。
|- Django版本:1.8
|- Python版本:3.4
models.py
1 class Story(models.Model): 2 3 #编辑Story状态 4 STATUS_CHOICES = ( 5 (1, ‘待编辑状态‘), 6 (2, ‘待批准状态‘), 7 (3, ‘已发布状态‘), 8 (4, ‘已存档‘), 9 ) 10 11 12 title = models.CharField(max_length=100, verbose_name=u‘文章‘) 13 slug = models.SlugField(verbose_name=u‘简短标题‘) 14 category =models.ForeignKey(Category, verbose_name=u‘分类‘) 15 owner = models.ForeignKey(User, verbose_name=u‘发布者‘) 16 status = models.IntegerField(choices=STATUS_CHOICES, default=1, verbose_name=u‘发布状态‘) 17 18 19 class Meta: 20 ordering = [‘modified‘,‘created‘] 21 verbose_name_plural = verbose_name = ‘文章‘ 22 23 24 def __str__(self): 25 return self.title
admin.py
1 class StoryAdmin(admin.ModelAdmin): 2 list_display = (‘title‘, ‘owner‘, ‘status‘, ‘created‘, ‘modified‘,‘read_times‘,‘top‘, ‘id‘) 3 search_fields = (‘title‘, ‘markdown_content‘) 4 # 状态选择器 5 list_filter = (‘status‘, ‘owner‘, ‘created‘, ‘modified‘,) 6 prepopulated_fields = {‘slug‘: (‘title‘,)} 7 actions = [‘make_story_public‘] 8 9 def make_story_public(self, request, queryset): 10 # queryset参数为选中的Story对象 11 rows_updated = queryset.update(status=3) 12 message_bit = "%s 篇文章" % rows_updated 13 self.message_user(request, "%s 已成功标记为已发布状态." % message_bit) 14 make_story_public.short_description = u‘修改选中文章为已发布状态‘
以上是关于Django学习笔记:为Model添加Action的主要内容,如果未能解决你的问题,请参考以下文章