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)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."))I would recommend you move this logic to the admin view, where
the request is going to be available at all times. Specifically,
you should register your own admin class and override the
save_model() method, as described here:
https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model.
Don't forget to call super() to actually save the model instance
before your custom logic!
--
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 mezzanine-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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)add_message() argument must be an HttpRequest object, not '__proxy__'.
<WSGIRequest: POST '/admin/.../'>return DisplayableAdmin.message_user(request, _("My custome message."), messages.WARNINGreturn self.message_user(request, _("My custome message."), messages.WARNING)If that's the case you may want to override response_change()
instead. This method is quite complex and determines which message
to display depending on what action the user just triggered (save,
save and continue, save and add new, etc).
https://docs.djangoproject.com/en/1.11/_modules/django/contrib/admin/options/#ModelAdmin.response_change
--