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.
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.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With