I got some strange behavior with formset validation for model form formset. I'am using Django 1.5, python 2.7 Here is code what i used.
Model class:
class WorkDone(models.Model):
task = models.ForeignKey(Task)
type_of_work = models.ForeignKey("pricelist.TypeOfWork")
Form class:
class WorkDoneForm(forms.ModelForm):
class Meta:
model = WorkDone
exclude = ("task",)
def __init__(self, *args, **kwargs):
super(WorkDoneForm, self).__init__(*args, **kwargs)
self.fields["type_of_work"].widget = forms.HiddenInput()
Formset class generation:
WorkDoneFormSet = formset_factory(WorkDoneForm, can_delete=True)
Formset creation in get_context_data function of view:
context["work_done_formset"] = context.get("work_done_formset", WorkDoneFormSet(prefix='work_done'))
So, i submit form with one WorkDone form in formset without any entered data(for typeOfWork of course) and here POST data from POST request:
QueryDict: {u'_save': [u''],
u'work_done-0-type_of_work': [u''],
u'work_done-INITIAL_FORMS': [u'0'],
u'work_done-TOTAL_FORMS': [u'1'],
u'csrfmiddlewaretoken': [u'nFkTCyx3413yrFE9XpNQDGdNlzPAHwyI'],
u'work_done-MAX_NUM_FORMS': [u'1000']}
And output from this code bellow in view:
work_done_formset = WorkDoneFormSet(request.POST, prefix="work_done")
print work_done_formset.is_valid(), work_done_formset.cleaned_data
for form in work_done_formset:
print form.is_valid(), form.cleaned_data
>> True [{}]
>> True {}
Why validation is true and why there is no error for missing TypeOfWork fields in form? Thank you.