Thanks again for your help Eduardo.
I have taken a look at the response_change method and I have done a test. 
This method isn't appropriate for my case because it isn't triggered when 
the change has made from the listing page. Change the status of the records 
(Draft/Published) will be the most frequent action in this model, and will 
be done mainly from the listing page. Then it's necessary that the method 
used is also triggered from the listing page.

Il giorno venerdì 13 ottobre 2017 22:36:13 UTC+2, Rainell Dilou Gómez ha 
scritto:
>
> Hello everyone,
> I need send a message to admin from the *save* *method* in a *model*, 
> something like that:
>
> class MyModel(Displayable, Ownable, RichText):
>
>     # some field
>
>     def save(self, *args, **kwargs):
>
>         # do something
>
>         # set the message to be displayed
>
>         super(MyModel, self).save(*args, **kwargs)
>
> Then, in the admin, the message should be displayed when the form is sent 
> (save).
>
> I have done a test after having read documentation, questions on 
> stackoverflow.com, etc. The test doesn't generate errors during the 
> execution but I don't get the result I want, the default Mezzanine message 
> is still displayed, my custom message isn't displayed.
> The code of my test would look like that:
>
> from django.db import models
> from django.utils.translation import ugettext_lazy as _
> from mezzanine.core.models import Displayable, Ownable, RichText
>
> from django.contrib import messages
> from django.test import RequestFactory
> from django.contrib.messages.storage.fallback import FallbackStorage
>
> def create_request(url):
>     factory = RequestFactory()
>     request = factory.get(url)
>     setattr(request, 'session', 'session')
>     messages = FallbackStorage(request)
>     setattr(request, '_messages', messages)
>     return request
>
> class MyModel(Displayable, Ownable, RichText):
>
>     # some field
>
>     def save(self, *args, **kwargs):
>
>         # do something
>
>         url = "/admin/path_to/the_instance/%s/change/" % self.id
>         request = create_request(url)
>         messages.error(request, _("My custom message."))
>
> As I said earlier, this does not generate errors, but the message is not 
> displayed.
> Please, how can I do it?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to