1
如果formset中的所有表單均有效,則基於inlineformset_factory創建的表單總是返回True。當formset中的表格未填寫時,它也返回True。沒有填寫意味着在這裏,用戶沒有插入至少一個值。至少這是我發現的。django inlineformset_factory - 如何判斷表單中的表單是否爲空
有沒有辦法瞭解formset中的表單是空的?例如,下面我不想設置用戶消息,即創建對象時,實際上沒有創建對象。
CashExpenditureFormSet = inlineformset_factory(Project, CashExpenditure, form=CashExpenditureForm, can_delete=False, extra=1, max_num=1)
if request.method == 'POST':
cash_expenditure_formset = CashExpenditureFormSet(request.POST, request.FILES, instance=project)
if cash_expenditure_formset.is_valid():
cash_expenditure_formset.save()
request.user.message_set.create(message='The Cash Expenditure was added successfully to project: "%s".' % project.__str__())
感謝。完美的答案。 – 2010-01-12 22:43:27