Hi,
I create a FormSet with modelformset_factory.
HourRecordFormSet = modelformset_factory(HourRecord, max_num=6, extra=3, form=HourRecordBatchForm)
Then I want to set the project field of a certain HourRecord object to the actual project. This should be done in the view. I tried it like this.
if hour_record_formset_january.is_valid():
#hour_record_formset_january.save()
for form in hour_record_formset_january.forms:
if form:
hour_record = form.save(commit=False)
hour_record.project = project
hour_record.save()
You see I commented out the hour_record_formset_january.save() since I want to add first the project to the hour_record object. This works fine. But for the extra forms I have a problem, since if they are not filled out I get an error.
Thus I want to save only the hour_record objects where at least only one field is filled out. This would be done with hour_record_formset_january.save(), but then I can not initialise another field.
I can not use a inlineformset_factory, since I have to call the HourRecordFormSet with a specific queryset. I do it like this:
january_qs = HourRecord.objects.filter(who_worked=get_current_user(), project=project, date_of_work__year=year, date_of_work__month=1)
hour_record_formset_january = HourRecordFormSet(queryset=january_qs)