如何强制保存“空”/未更改的 django 管理内联?

发布于 2024-09-18 05:14:15 字数 236 浏览 1 评论 0原文

我的一个管理模型中有一些内联,它们具有默认值,在使用“添加另一个...”添加新实例时可能不需要更改这些默认值。不幸的是,除非某些值发生了变化,否则 django 不会将这些内联对象识别为新对象。这迫使我添加一个内联,更改任意值,保存,更改回该值并再次保存以达到所需的效果。

到目前为止,我想到的唯一解决方案是添加一个隐藏的“已更改”字段,在添加新的内联时将通过 JavaScript 填充该字段。因为这感觉很黑客,我希望有一个更优雅的解决方案。

I have some inlines in one of my admin models which have default values which likely won't need to be changed when adding a new instance with "Add another ...". Unfortunately django won't recognize these inline as new objects unless some value has changed. This forces me to add an inline, change an arbitrary value, save, change the value back and save again to reach the desired effect.

The only solution I've come up with so far is to add a hidden 'changed'-field which would be populated via JavaScript when adding a new inline. As this feels very hackish I hope there is a more elegant solution.

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

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

发布评论

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

评论(3

冷默言语 2024-09-25 05:14:15

我花了很长时间才弄清楚,但实际上非常简单。

from django.contrib import admin
from django.forms.models import BaseInlineFormSet, ModelForm

class AlwaysChangedModelForm(ModelForm):
    def has_changed(self):
        """ Should returns True if data differs from initial. 
        By always returning true even unchanged inlines will get validated and saved."""
        return True

class CheckerInline(admin.StackedInline):
    """ Base class for checker inlines """
    extra = 0
    form = AlwaysChangedModelForm

It took me quite some time to figure out but it is actually really simple.

from django.contrib import admin
from django.forms.models import BaseInlineFormSet, ModelForm

class AlwaysChangedModelForm(ModelForm):
    def has_changed(self):
        """ Should returns True if data differs from initial. 
        By always returning true even unchanged inlines will get validated and saved."""
        return True

class CheckerInline(admin.StackedInline):
    """ Base class for checker inlines """
    extra = 0
    form = AlwaysChangedModelForm
掌心的温暖 2024-09-25 05:14:15

@daniel 答案很好,但是如果没有进行任何更改,它会尝试保存已经创建的实例,这是没有必要的,最好像这样使用它:

class AlwaysChangedModelForm(ModelForm):
    def has_changed(self, *args, **kwargs):
        if self.instance.pk is None:
            return True
        return super(AlwaysChangedModelForm, self).has_changed(*args, **kwargs)

@daniel answer is great, however it will try to save the instance that is already created ever if no changes is made, which is not necessary, better to use it like:

class AlwaysChangedModelForm(ModelForm):
    def has_changed(self, *args, **kwargs):
        if self.instance.pk is None:
            return True
        return super(AlwaysChangedModelForm, self).has_changed(*args, **kwargs)
赏烟花じ飞满天 2024-09-25 05:14:15

@Daniels、@HardQuestions' 和 @jonespm 的综合答案:

class MarkNewInstancesAsChangedModelForm(forms.ModelForm):
    def has_changed(self):
        """Returns True for new instances, calls super() for ones that exist in db.
        Prevents forms with defaults being recognized as empty/unchanged."""
        return not self.instance.pk or super().has_changed()

Combined @Daniels, @HardQuestions' and @jonespm answers:

class MarkNewInstancesAsChangedModelForm(forms.ModelForm):
    def has_changed(self):
        """Returns True for new instances, calls super() for ones that exist in db.
        Prevents forms with defaults being recognized as empty/unchanged."""
        return not self.instance.pk or super().has_changed()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文