Django learning notes: Add Action for Model and django learning notes
|-Django version: 1.8
|-Python version: 3.4
Models. py
1 class Story (models. model): 2 3 # edit the Story status 4 STATUS_CHOICES = (5 (1, 'awaiting editing status'), 6 (2, 'pending approval status'), 7 (3, 'published status'), 8 (4, 'archived '), 9) 10 11 12 title = models. charField (max_length = 100, verbose_name = U' ') 13 slug = models. slugField (verbose_name = u 'short title') 14 category = models. foreignKey (Category, verbose_name = u'category') 15 owner = models. foreignKey (User, verbose_name = u'publisher ') 16 status = models. integerField (choices = STATUS_CHOICES, default = 1, verbose_name = u'publish state') 17 18 19 class Meta: 20 ordering = ['modified ', 'created '] 21 verbose_name_plural = verbose_name = 'Article '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 # status selector 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 # The queryset parameter is the selected Story object 11 rows_updated = queryset. update (status = 3) 12 message_bit = "% s article" % rows_updated13 self. message_user (request, "% s has been successfully marked as published. "% message_bit) 14 make_story_public.short_description = u 'modify the selected Article to published status'