我不得不重载django admin中的save_formset方法。当试图检测是否删除?复选框已选中,它始终返回True。
我有一个内联管理类: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因为删除总是打印,不确定如何实际检测删除字段的值是什么?是
注意
form.fields ['DELETE']字段是一个BooleanField。我错过了使用BooleanFields的简单方法吗?我试图将它包装在bool()中,但它总是返回True。
答案 0 :(得分:1)
您应该检查cleaned_data
字典中的表单数据,而不是fields
:
if 'DELETE' in form.fields and form.cleaned_data.get('DELETE'):