Skip to content

Commit

Permalink
[3.0.x] Improved message example in admin actions documentation.
Browse files Browse the repository at this point in the history
Avoid partial string construction and make use of ``ngettext`` to show
example of how to handle plural variants with translations. Also make
use of ``messages.SUCCESS`` to highlight customizing the style of the
message - in this case it better fits what the message is conveying.
Backport of 058b38b from master
  • Loading branch information
ngnpope authored and carltongibson committed Apr 16, 2020
1 parent e42320d commit 70b1c94
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions docs/ref/contrib/admin/actions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,19 @@ provided by the admin.
For example, we can use ``self`` to flash a message to the user informing her
that the action was successful::

from django.contrib import messages
from django.utils.translation import ngettext

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)
updated = queryset.update(status='p')
self.message_user(request, ngettext(
'%d story was successfully marked as published.',
'%d stories were successfully marked as published.',
updated,
) % updated, messages.SUCCESS)

This make the action match what the admin itself does after successfully
performing an action:
Expand Down

0 comments on commit 70b1c94

Please sign in to comment.