How to prevent "Changed successfully" message when overriding save_model method
You can hide certain messages and show others. In your case, you are interested in not displaying the the success message. You can do the following:
def save_model(self, request, obj, form, change):
messages.set_level(request, messages.ERROR)
messages.error(request, 'No changes are permitted ..')
If you want to prevent success message in case of failure message then you must have to set the error level of the messages.
Something like this ->
if already_exist:
messages.set_level(request, messages.ERROR)
messages.error(request, 'a and b mapping already exist. Please delete the current entry if you want to override')
else:
obj.save()
It will avoid coming two messages (Success and Failure) at the same time because we have suppressed success message by changing the error level.
For anyone who needs to eliminate the automatic Django success message in a more flexible way than proposed by the accepted answer, you can do the following:
from django.contrib import messages, admin
class ExampleAdmin(admin.ModelAdmin):
def message_user(self, *args):
pass
def save_model(self, request, obj, form, change):
super(ExampleAdmin, self).save_model(request, obj, form, change)
if obj.status == "OK":
messages.success(request, "OK!")
elif obj.status == "NO":
messages.error(request, "REALLY NOT OK!")