Hi Eduardo,
I have tried as you suggested, my new test would be something like this:

from django.contrib import messages
from django.utils.translation import ugettext_lazy as _
from mezzanine.core.admin import DisplayableAdmin, OwnableAdmin

class MyModelAdmin(DisplayableAdmin, OwnableAdmin):

    # fieldsets, list_display, etc.

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

        # do something

        return DisplayableAdmin.message_user(request, _("My custome 
message."), messages.ERROR)

but now I get the following error:

add_message() argument must be an HttpRequest object, not '__proxy__'.


to see what contains the request object, I have used *raise 
Exception(request)* and got

<WSGIRequest: POST '/admin/.../'>

Please, what could I do now?


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