Thomas Kremmel
Thomas Kremmel

Reputation: 14783

django inlineformset_factory - how to judge whether a form out of the formset was empty

A formset created based on the inlineformset_factory always returns True if all forms in the formset are valid. It returns also True when the forms in the formset are not filled out. Not filled out means here, that the user did not inserted at least one value. At least this is what I discovered.

Is there a way to get to know that a form in the formset was empty? For example, below I do not want to set the user message, that the object was created, when in fact the object was not created.

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__())

Upvotes: 1

Views: 886

Answers (1)

Dan Breen
Dan Breen

Reputation: 12924

This is very closely related to an answer I posted on another question, having dealt with this problem myself: Inline Form Validation in Django

Upvotes: 3

Related Questions