我正在使用django formset一次保存多个表单。但是我的数据库中没有保存数据。我正在使用class based
个视图。这是我要保存数据的模型。
模型
class Period(AbstractBaseModel):
validators = [ScreenMethodValidator, ChainIntegrityValidator]
chain = models.ForeignKey(
'customers.Chain',
help_text=_('chain to which period belongs to'),
verbose_name=_('chain')
)
start_date = models.DateTimeField(
default=datetime.datetime.now,
help_text=_('Starting date of a period'),
verbose_name=_('start date')
)
end_date = models.DateTimeField(
default=datetime.datetime(1970, 01, 01),
help_text=_('Ending date of a period'),
verbose_name=_('end date')
)
year = models.IntegerField(
default=0,
help_text=_('Financial year'),
verbose_name=_('year'),
null=True,blank=True
)
description = models.TextField(
help_text=_('Description of a period'),
verbose_name=_('description'),
null=True,blank=True
)
class Meta:
app_label = 'accounting'
和查看
class PeriodCreate(RequestPassingFormViewMixin, WammuCreateView):
model = Chain
template_name = 'dashboard/period_form.html'
form_class = ChainForm
def get_object(self):
chain = Chain.objects.get(pk=self.kwargs['chain_pk'])
return chain
def get_success_url(self):
return reverse('dashboard_period_list', kwargs={'chain_pk': self.object.chain.id, })
def get_context_data(self, **kwargs):
context = super(PeriodCreate, self).get_context_data(**kwargs)
return context
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(PeriodCreate, self).get_form_kwargs(*args, **kwargs)
chain = get_object_or_404(Chain, pk=self.kwargs['chain_pk'])
period = Period(chain=chain)
kwargs['instance'] = period
return kwargs
def get(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
PeriodInlineFormSet = inlineformset_factory(Chain, Period,
form=PeriodInlineForm,
can_delete=True,
extra=12)
PeriodInlineFormSet.form = staticmethod(curry(PeriodInlineForm, request=request, chain=self.object))
period_formset = PeriodInlineFormSet()
return self.render_to_response(
self.get_context_data(form=form,
period_inline_formset=period_formset))
def post(self, request, *args, **kwargs):
self.object = Chain()
form = self.get_form(self.form_class)
PeriodInlineFormSet = inlineformset_factory(Chain, Period,
form=PeriodInlineForm,
can_delete=True,
extra=12)
PeriodInlineFormSet.form = staticmethod(curry(PeriodInlineForm))
if form.is_valid():
self.object = form.save(commit=False)
period_formset = PeriodInlineFormSet(request.POST, instance=self.object)
if period_formset.is_valid():
self.object.save()
period_formset.save()
return super(PeriodCreate, self).form_valid(form)
else:
return self.render_to_response(
context=self.get_context_data(form=form, period_inline_formset=period_formset))
else:
period_formset = PeriodInlineFormSet(request.POST,instance=self.object)
return self.render_to_response(
context=self.get_context_data(form=form, period_inline_formset=period_formset))
和使用以下链接查看与此问题相关的其他代码我尝试使用django formset并遇到已解决的参数错误
但是当我尝试发布数据时,现在数据没有保存在数据库中。但是我的post
方法中发生了错误,但由于我不习惯使用django formset,我可以&#39经过长时间的调查后弄清楚了。