33

I want to display a message to admins after they save a particular model, something like "Now enable the series".

I can see how I'd do this if it were a list action (message_user) but I can't see how to do this from the main CRUD form.

Does anyone know how?

Thanks.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Roger
  • 4,911
  • 6
  • 29
  • 26
  • I had a message when a user saved an entry to the admin (yellow background on top of the page), but somehow it disappeared. How do I get it back? Now I only get a message when a user did an error – Timo May 28 '14 at 12:20

4 Answers4

73

Old question, but worth at least a small example as I think this is quite a common issue.

@Davor Lucic pointed to the right solution. As of today, Django ships with a cool message framework that helps a lot with this.

So, say you want to give notice within the Django Admin whenever a car object within your Car model changes owner, you could do something like that:

admin.py

from django.contrib import admin
from django.contrib import messages

from .models import Car


@admin.register(Car)
class CarAdmin(admin.ModelAdmin):
    list_display = ('owner', 'color', 'status', 'max_speed', )

    def save_model(self, request, obj, form, change):
        if 'owner' in form.changed_data:
            messages.add_message(request, messages.INFO, 'Car has been sold')
        super(CarAdmin, self).save_model(request, obj, form, change)

It's worth mentioning that if you want to include HTML tags in your message, you have to add:

from django.utils.safestring import mark_safe

which allows you to do something like:

messages.add_message(request, messages.INFO, mark_safe("Please see <a href='/destination'>here</a> for further details"))

No need to say you'd better be sure the code you're adding is REALLY safe.

Nothing exceptional, but maybe (and hopefully) someone will find it useful.

Community
  • 1
  • 1
Seether
  • 1,524
  • 1
  • 14
  • 28
  • 7
    Instead of `mark_safe`, use the [`format_html`](https://docs.djangoproject.com/en/dev/ref/utils/#module-django.utils.html) (`from django.utils.html import format_html`). – nik_m Dec 28 '16 at 09:32
  • 3
    This is working for me but the issue is I see the inbuilt message as well, I want to return error message only but it shows both error and success message. Success message is coming from django. I tried return when I see error but still see both the messages. – Uday Swami Nov 10 '17 at 08:43
16

You can override save_model method on ModelAdmin so you can add message using message framework after you save your object.

Davor Lucic
  • 28,970
  • 8
  • 66
  • 76
9

@Seether answer is right but it display 2 messages as mentioned by @Uday in comment.

Display only one custom message

Answered by @Dylan For anyone who needs to eliminate the automatic Django success message in a more flexible way or

from django.contrib import messages

class SomeAdmin(admin.ModelAdmin):
      def message_user(self, *args): # overridden method
            pass

      def save_model(self, request, obj, form, change):

          messages.add_message(request, messages.INFO, 'Custom message goes here')
  • Excellent thanks! I find that overriding `message_user` like you do also works with `messages.success` (or `warning`, etc) instead of `messages.add_message`, which is equivalent. – cjauvin Nov 02 '20 at 17:43
0

Overriding "save_model()", you can set and show a custom message and don't forget to import "messages" as shown below:

from django.contrib import admin
from .models import MyModel
from django.contrib import messages # Here

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    # Here
    def save_model(self, request, obj, form, change):
        obj.save()
        messages.add_message(request, messages.INFO, 'Custom Message')
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129