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!
Also, keep in mind ModelAdmin provides message_user() as a convenience
to display messages in the admin.
https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.ModelAdmin.message_user
On 2017-10-13 2:36 PM, Rainell Dilou Gómez wrote:
Hello everyone,
I need send a message to admin from the /save/ /method/ in a /model/,
something like that:
|
classMyModel(Displayable,Ownable,RichText):
# some field
defsave(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:
|
fromdjango.db importmodels
fromdjango.utils.translation importugettext_lazy as_
frommezzanine.core.models importDisplayable,Ownable,RichText
fromdjango.contrib importmessages
fromdjango.test importRequestFactory
fromdjango.contrib.messages.storage.fallback importFallbackStorage
defcreate_request(url):
factory =RequestFactory()
request =factory.get(url)
setattr(request,'session','session')
messages =FallbackStorage(request)
setattr(request,'_messages',messages)
returnrequest
classMyModel(Displayable,Ownable,RichText):
# some field
defsave(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]
<mailto:[email protected]>.
For more options, visit https://groups.google.com/d/optout.
--
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.