Django 内联管理删除复选框始终 returns true

Django's Inline Admin's Delete checkbox always returns true

我不得不在我的 django 管理中重载 save_formset 方法。当试图检测是否删除?复选框已选中,它始终 returns 正确。

我有一个内联管理员 class:DataPoolInline 由以下内容内联:DataAdmin

在 DataAdmin 内部,我重载了 save_formset:

  def save_formset( self, request, form, formset, change):
    formset.save()
    idx = 0
    for f in formset.forms:
      if isinstance( f.instance, DataPool ):
        DataPool.save_model( self, request, f.instance, f, change, idx )
        idx += 1

静态方法内部DataPool.save_model:

  #Used to save a model from the admin
@staticmethod
def save_model( adm, request, obj, form, change=None, idx=None ):
    #If delete was in there, then stop
  if 'DELETE' in form.fields and form.fields['DELETE']:
    print("Quiting because delete is selected")
    return

    #Save my object
  obj.save()

Quiting 因为delete 总是被打印出来,不确定如何实际检测到Delete 字段的值是什么?是。

备注

form.fields['DELETE'] 字段是一个布尔字段。我是否缺少使用 BooleanFields 的简单操作?我试图将它包装在 bool() 中,但它总是 returns True.

您应该检查 cleaned_data 字典中的表单数据而不是 fields:

if 'DELETE' in form.fields and form.cleaned_data.get('DELETE'):