Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 262
zbar Avatar asked Jul 13 '10 21:07

zbar


People also ask

Is Django's admin interface customizable if yes then how?

In this article, we will discuss how to enhance Django-admin Interface. Let us create an app called state which has one model with the same name(state). When we register app to admin.py it shows like. Now lets' customize django admin according to available options.

What is admin Modeladmin in Django?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.

How can I remove extra's from Django admin panel?

Take a look at the Model Meta in the django documentation. Within a Model you can add class Meta this allows additional options for your model which handles things like singular and plural naming. Show activity on this post. inside model.py or inside your customized model file add class meta within a Model Class.


1 Answers

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!

like image 144
Bartek Avatar answered Sep 23 '22 20:09

Bartek