Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display custom message from signal in the admin

Tags:

django

I have a pre-save signal listener that updates a second model. The same as this example:

  • Django Signals to update a different model

I'd like to let the user know that the listener succeeded in updating the model and provide some information. Normally, I would think I could use the built in messages functionality that django has. The problem is that the signal doesn't have access to 'request'. So I can't see how to use the built in Django Messages Framework.

  • https://docs.djangoproject.com/en/dev/ref/contrib/messages/

Is there a known method for sending a message to the user in the admin? Maybe by overriding the save() method for one of the models? (the one sending the signal, or receiving), but I don't think the save() method has access to 'request' either?

This must be something others want to do as well?

like image 967
Jglstewart Avatar asked Dec 20 '12 03:12

Jglstewart


1 Answers

You can override save_model method in ModelAdmin. Something like this:

from django.contrib import messages
# your imports
...
# your code

def save_model(self, request, obj, form, change):
    obj.user = request.user  
    obj.save()
    # you can just call super(YourModelAdminName, self).save_model(request, obj, form, change)
    messages.add_message(request, messages.INFO, 'Text of message')
like image 80
Max Avatar answered Sep 28 '22 05:09

Max