Django:在成功保存表单后自定义消息

发布于 2024-10-09 11:11:20 字数 494 浏览 1 评论 0原文

每当我在管理界面中保存模型时,它都会显示通常的“成功保存消息”。 但是,我想知道是否可以自定义此消息,因为我想警告用户他刚刚保存的内容以及这些操作的含义。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋凉 2024-10-16 11:11:20

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

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)

要检测正在保存的对象的更改,您应该覆盖 ModelAdmin的save_model方法,并将该方法传递的对象与当前数据库中的版本进行比较。要在内联的情况下执行此操作,您可以覆盖 save_formset方法。一种可能的方法可能如下所示(未经测试的代码):

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 (> 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()
執念 2024-10-16 11:11:20

如果您使用的是 Django 1.2 或更高版本,消息框架可能会提供答案。

http://docs.djangoproject.com/en/dev/ref/contrib/消息/

If you're using Django 1.2 or newer, the messages framework may hold the answer.

http://docs.djangoproject.com/en/dev/ref/contrib/messages/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文