如何在django admin

时间:2017-06-29 11:50:18

标签: python django django-forms django-admin

以下是django源代码中的save_related()代码

def save_related(self, request, form, formsets, change):
    """
    Given the ``HttpRequest``, the parent ``ModelForm`` instance, the
    list of inline formsets and a boolean value based on whether the
    parent is being added or changed, save the related objects to the
    database. Note that at this point save_form() and save_model() have
    already been called.
    """
    form.save_m2m()
    for formset in formsets:
        self.save_formset(request, form, formset, change=change)

我不希望我的表单在admin中为我的内联调用save_formset,所以我需要覆盖save_related()方法以避免它。

我试过但没找到正确的方法。

1 个答案:

答案 0 :(得分:1)

您应该覆盖save_formset方法,如下所示:

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

这样,每次通过管理员保存主对象时,内联都不会保存到数据库中。