我有一个表单和formset。我想同时更新它们(使用相同的提交)。表单呈现正确(具有良好的数据),但更新的数据不会写入db。虚空形成验证工作。当它是一种形式时,它是完美的。哪个可能是问题?
我的观点类:
class EventAdminPage(UpdateView):
form_class = CreateEventForm
second_form_class = modelformset_factory(EventTime, form = EventTimeForm, min_num=1, validate_min=True, extra=3)
model = Event
template_name = 'eventAdmin.html'
def get_context_data(self, **kwargs):
context = super(EventAdminPage, self).get_context_data(**kwargs)
event_times = EventTime.objects.filter(event = self.object).exclude(start_time = None)
context['participants'] = Participant.objects.filter(event = self.object)
context['event_times'] = EventTime.objects.filter(event = self.object).exclude(start_time = None)
context['event_time_formset_helper'] = event_time_formset_helper = EventTimeFormsetHelper()
context['formset'] = self.second_form_class(prefix='eventtime', queryset=event_times)
context['form'] = self.form_class(instance = self.object)
return context
def post(self, request, **kwargs):
self.object = self.get_object()
event_times = EventTime.objects.filter(event = self.object).exclude(start_time = None)
if 'update_event_form' in request.POST:
form = self.form_class(instance=self.get_object())
formset = self.second_form_class(queryset=event_times)
if form.is_valid() and formset.is_valid():
form.save()
formset.save()
return HttpResponseRedirect('/event-admin-%s' %self.kwargs['event_id'])
else:
return self.render_to_response(
self.get_context_data(form=form, formset=formset))
def get(self, request, **kwargs):
self.object = Event.objects.get(pk=self.kwargs['event_id'])
form = self.form_class
formset = self.second_form_class
context = self.get_context_data(object=self.object, form=form, formset=formset)
return self.render_to_response(context)
def get_object(self, queryset=None):
obj = Event.objects.get(pk=self.kwargs['event_id'])
return obj