Question

Custom error message in Django admin actions

I've written custom admin actions that basically do QuerySet.update() for certain fields in the model. There are times when these actions shouldn't be allowed to complete -- instead, they should display an error and not do anything. I've tried message_user, but that displays a green checkmark, whereas I'd like it to display the Django admin error message.

A solution I've found online is to use a ModelForm, but I don't think that applies in this case, as here everything happens on the admin change list page.

 45  41172  45
1 Jan 1970

Solution

 53

The message_user function used within the admin simply uses the contrib.messages package. You could try something like this:

from django.contrib import messages

# Then, when you need to error the user:
messages.error(request, "The message")

You can also use warning, debug, info and success in place of error

Hope that helps!

2010-07-13

Solution

 53
from django.contrib import messages
...
self.message_user(request, "The message", level=messages.ERROR)

Сan also be used (messages.ERROR, messages.WARNING, messages.DEBUG, messages.INFO, messages.SUCCESS)

2013-07-03