此问题与此问题相关:Django - disable model editing
这是我的代码:
def save(self, *args, **kwargs):
if self.pk is None:
print "THIS IS ADD ACTION. NOT DELETE OR CHANGE !!!"
numberOfAvalableBooks = Book.objects.get(id=self.book.id).amount
print 'numberOfAvalableBooks %s' % numberOfAvalableBooks
if self.get_action_display() == 'Out':
if numberOfAvalableBooks - self.amount < 0:
return "YOU DO NOT HAVE ENOUGH BOOKS !!!"
super(Transaction, self).save(*args, **kwargs)
# UPDATE AMOUNT
book = Book.objects.get(id=self.book.id)
if self.get_action_display() == 'Out':
book.amount -= self.amount
elif self.get_action_display() == 'In':
book.amount += self.amount
else:
print "UNKNOWN ACTION !!!"
book.save()
else:
print self.pk
return "CHANGE ACTION DISABLED !!!"
此工作正常,无法更新或删除 我不喜欢的是,在点击“保存”按钮后,我仍然有一条黄色的消息,其中“......已成功添加。”。
是否有可能以及如何替换该消息并使其具有不同的颜色?
由于
答案 0 :(得分:1)
覆盖管理类的response_add(self, request, obj, post_url_continue=None)
方法。
查看django/contrib/admin/options.py
以下是模型管理员的一个非常基本的示例,它在添加和更改实例后覆盖消息。
class AmodelAdmin(admin.ModelAdmin):
...
def response_add(self, request, obj, post_url_continue=None):
from django.core.urlresolvers import reverse
msg = "DON'T CLICK THIS BUTTON!"
self.message_user(request, msg, level=messages.WARNING)
return self.response_post_save_add(request, obj)
def response_change(self, request, obj):)
msg = "DON'T CLICK THIS BUTTON!"
self.message_user(request, msg, level=messages.WARNING)
return self.response_post_save_change(request, obj)