1

场景:

我有这样的模型:

class A(models.Model):
    ...

class B(models.Model):
    a = models.ForeignKey(A)
    ...

在管理员我有这样的事情:

class AInlinde(admin.StackedInline):
    ...
    model = A
    ...

class BAdmin(admin.ModelAdmin):
     ...
     inlines = [AInline]
     ...

现在我需要在用户删除内联中的某些模型时记录在日志中,但是我无法按照代码进行操作,直到现在,我知道Badmin有一个方法:save_formset,就是这样:

def save_formset(self, request, form, formset, change):
    """
    Given an inline formset save it to the database.
    """
    formset.save()

但我认为这formset是通过方法生成的modelformset_factory,所以我不知道什么时候可以覆盖该表单集的删除行为

4

3 回答 3

3

对于任何在 2017 年寻找软删除和记录删除的人。如今在 django 1.11 中,您可以覆盖delete_existing方法

对于常规表单集

class SoftDeleteModelFormSet(BaseModelFormSet):
    def delete_existing(self, obj, commit=True):
        """Deletes an existing model instance."""
        if commit:
            obj.deleted_at = datetime.datetime.now()
            obj.save()

            # log here

对于相关型号

class SoftDeleteInlineFormSet(BaseInlineFormSet):
    def delete_existing(self, obj, commit=True):
        """Deletes an existing model instance."""
        if commit:
            obj.deleted_at = datetime.datetime.now()
            obj.save()

            # log here

并将 modelformset_factory 与新的表单集一起使用

product_formset = modelformset_factory(
    ...
    formset = SoftDeleteModelFormSet,
    ...
)
于 2017-07-27T04:09:18.583 回答
2

覆盖表单集如何保存或删除对象的最佳方法是创建一个继承BaseInlineFormSet并覆盖方法save和的类save_existing_objects

class NoDeleteFormset(BaseInlineFormSet):
    def save(self, commit=True, request=None):
        """Saves model instances for every form, adding and changing instances
        as necessary, and returns the list of instances.
        """
        if not commit:
            self.saved_forms = []
            def save_m2m():
                for form in self.saved_forms:
                    form.save_m2m()
            self.save_m2m = save_m2m
        return self.save_existing_objects(commit) + self.save_new_objects(commit)

    def save_existing_objects(self, commit=True, request=None):
        self.changed_objects = []
        self.deleted_objects = []
        if not self.initial_forms:
            return []

        saved_instances = []
        for form in self.initial_forms:
            pk_name = self._pk_field.name
            raw_pk_value = form._raw_value(pk_name)

            # clean() for different types of PK fields can sometimes return
            # the model instance, and sometimes the PK. Handle either.
            pk_value = form.fields[pk_name].clean(raw_pk_value)
            pk_value = getattr(pk_value, 'pk', pk_value)

            obj = self._existing_object(pk_value)
            if self.can_delete and self._should_delete_form(form):
                self.deleted_objects.append(obj)
                obj.deleted_at = datetime.now()
                obj.save()

                # log there your user deleting the object!

                continue
            if form.has_changed():
                self.changed_objects.append((obj, form.changed_data))
                saved_instances.append(self.save_existing(form, obj, commit=commit))
                if not commit:
                    self.saved_forms.append(form)
        return saved_instances

然后构造一个比继承自的类StackedInline

class MetaInline(StackedInline):
    formset = NoDeleteFormset
    ...
于 2012-06-10T16:31:20.177 回答
0

您不需要自定义默认ModelAdmin删除行为,只需连接到django.db.models.signals.post_delete模型并在模型被删除后获取一个钩子并将其记录在那里。

于 2012-05-27T07:26:56.923 回答