检查Django admin.py中的当前消息

时间:2015-07-22 14:18:14

标签: python django django-models

使用Django 1.5,Python 2.7

有没有办法可以检查已添加到admin.py文件中的邮件对象的邮件?在我的管理模型中,我正在覆盖save_model函数,并且如果它不符合某些条件,则希望停止“模型成功更改”消息。

from django.contrib import admin, messages

class ExampleAdmin(admin.ModelAdmin):
    list_display = ('whatever', 'etc')

    def save_model(self, request, obj, form, change):
        obj.save()
        # I want to look at messages right here

1 个答案:

答案 0 :(得分:0)

使用get_messages

示例:

from django.contrib import admin, messages


class ExampleAdmin(admin.ModelAdmin):
    list_display = ('whatever', 'etc')

    def save_model(self, request, obj, form, change):
        obj.save()
        storage = messages.get_messages(request)
        for message in storage:
            # Examine message here.
            pass