Django:成功保存表单后自定义消息 [英] Django: customizing the message after a successful form save

查看:416
本文介绍了Django:成功保存表单后自定义消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在管理界面中保存模型时,它将显示通常的成功保存的消息。
但是,我想知道是否可以自定义此消息,因为我有一个情况,我想要警告用户关于他刚才保存的内容以及这些操作的含义。

  class PlanInlineFormset(forms.models.BaseInlineFormset):
def clean(self):
## #如何检测更改?
###(self.changed_data不工作,因为它是一个内联)
###并显示他/她刚刚更改在成功保存后的顶部?

class PlanInline(admin.TabularInline):
model = Plan
formset = PlanInlineFormset


解决方案

Django(> version 1.2)使用消息框架来管理消息。您可以使用该界面添加其他消息。以下是一个例子:

 从django.contrib导入消息

class SomeModelAdmin(admin.ModelAdmin):
#你的正常ModelAdmin的东西在这里

def save_model(self,request,obj,form,change):
#添加一条额外的消息
messages.info请求,额外的消息在这里。)
super(SomeModelAdmin,self).save_model(request,obj,form,change)

要检测对保存对象的更改,您应该覆盖 save_model方法,并将方法传递给当前在数据库中的版本进行比较。为了在内联的情况下执行此操作,您可以覆盖 save_formset方法。一个可能的方法可能看起来像(未经测试的代码):

  class SomeModelAdmin(admin.ModelAdmin):
#你的正常ModelAdmin的东西在这里

def save_formset(self,request,form,formset,change):
如果不更改:
formset.save()
else:
instances = formset.save(commit = False)

例如在实例中:
try:
#如果你有多种类型的内联
#确保从
#适当的模型类型获取
old_object = SomeOtherModel.get(id = instance.id)
除了SomeOtherModel.DoesNotExist:
continue

if instance.field_x!= old_object.field_x:
messages.info(request,Something Changed)

instance.save()

FO rmset.save_m2m()


whenever I save a model in my Admin interface, it displays the usual "successfully saved message." However, I want to know if it's possible to customize this message because I have a situation where I want to warn the user about what he just saved and the implications of these actions.

class PlanInlineFormset(forms.models.BaseInlineFormset):
    def clean(self):
        ### How can I detect the changes?  
        ### (self.changed_data doesn't work because it's an inline)
        ### and display what he/she just changed at the top AFTER the successful save?

class PlanInline(admin.TabularInline):
    model = Plan
    formset = PlanInlineFormset

解决方案

Django (> version 1.2) uses the messages framework for admin messages. You can add additional messages using that interface. Here's an example:

from django.contrib import messages

class SomeModelAdmin(admin.ModelAdmin):
    # your normal ModelAdmin stuff goes here

    def save_model(self, request, obj, form, change):
        # add an additional message
        messages.info(request, "Extra message here.")
        super(SomeModelAdmin, self).save_model(request, obj, form, change)

To detect changes to the object being saved, you should be to override the save_model method of ModelAdmin, and compare the object the method is passed to the version currently in the database. To do this in the case of inlines, you can override the save_formset method. A possible approach might look like (untested code):

class SomeModelAdmin(admin.ModelAdmin):
    # your normal ModelAdmin stuff goes here

    def save_formset(self, request, form, formset, change):
        if not change:
            formset.save()
        else:
            instances = formset.save(commit=False)

            for instance in instances:
                try:
                    # if you've got multiple types of inlines
                    # make sure your fetching from the 
                    # appropriate model type here
                    old_object = SomeOtherModel.get(id=instance.id)
                except SomeOtherModel.DoesNotExist:
                    continue

                if instance.field_x != old_object.field_x:
                    messages.info(request, "Something Changed")

            instance.save()

        formset.save_m2m()

这篇关于Django:成功保存表单后自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆