Code

Opened 4 years ago

Closed 4 years ago

#12277 closed (wontfix)

Admin action example should use verbose_name in message_bit

Reported by: zanuxzan Owned by: nobody
Component: Documentation Version: master
Severity: Keywords: admin action
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Within http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#actions-as-modeladmin-methods it contains the following example;

class ArticleAdmin(admin.ModelAdmin):
    ...

    def make_published(self, request, queryset):
        rows_updated = queryset.update(status='p')
        if rows_updated == 1:
            message_bit = "1 story was"
        else:
            message_bit = "%s stories were" % rows_updated
        self.message_user(request, "%s successfully marked as published." % message_bit)

However, a DRY approach would be to use the models meta verbose_name and verbose_name_plural, ie:

from django.utils.encoding import force_unicode

class ArticleAdmin(admin.ModelAdmin):
    ...

    def make_published(self, request, queryset):
        rows_updated = queryset.update(status='p')
        if rows_updated == 1:
            message_bit = "1 %s was" % force_unicode(self.model._meta.verbose_name)
        else:
            message_bit = "%s %s were" % (rows_updated, force_unicode(self.model._meta.verbose_name_plural))
        self.message_user(request, "%s successfully marked as published." % message_bit)

That is of course unless there is a better way of doing this with a method?

Attachments (0)

Change History (2)

comment:1 Changed 4 years ago by timo

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from Admin action should to Admin action example should use verbose_name in message_bit

comment:2 Changed 4 years ago by russellm

  • Resolution set to wontfix
  • Status changed from new to closed

I'm not convinced that this change improves the documentation. The purpose of documentation is to be easy to understand - this introduces an esoteric, unrelated feature for no particular reason.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.