我想更新inlineformset_factory中所做的更改,而不是删除它并添加一个新的更改。
models.py
class invoice(models.Model):
invoice_no = models.CharField(max_length=20)
invoice_date = models.DateField(auto_now=False, auto_now_add=False)
class invoice_description(models.Model):
invoice = models.ForeignKey(invoice, related_name='items',
unique=False, on_delete=models.CASCADE)
service_description = models.CharField(max_length=50, blank=False)
hsn = models.CharField(max_length=50, blank=False)
qty = models.IntegerField(blank=False)
per = models.CharField(max_length=50, blank=False)
rate = models.IntegerField(blank=False)
currency = models.CharField(max_length=50, blank=False)
ex_rate = models.IntegerField(blank=False)
amount = models.IntegerField(blank=False)
gst = models.IntegerField(blank=False)
forms.py
class InvoiceForm(forms.ModelForm):
class Meta:
model = invoice
fields = ['invoice_no', 'invoice_date']
widgets = {
'invoice_no': forms.TextInput(attrs={'class': 'form-control'}),
'invoice_date': forms.DateInput(attrs={'type': 'date', 'class': 'form-control'}, format="%Y-%m-%d"),
}
class InvoiceDesForm(forms.ModelForm):
class Meta:
model = invoice_description
fields = '__all__'
widgets = {
'service_description': forms.TextInput(attrs={'size': 20}),
'hsn': forms.TextInput(attrs={'size': 5}),
'qty': forms.NumberInput(attrs={'class': 'form-number1'}),
'per': forms.TextInput(attrs={'size': 5}),
'currency': forms.TextInput(attrs={'size': 5}),
'rate': forms.NumberInput(attrs={'class': 'form-number1'}),
'ex_rate': forms.NumberInput(attrs={'class': 'form-number1'}),
'amount': forms.TextInput(attrs={'size': 5}),
'gst': forms.NumberInput(attrs={'class': 'form-number'}),
}
InvoiceFormSet = inlineformset_factory(发票,发票说明, form = InvoiceDesForm,extra = 5,can_delete = True)
views.py
class InvoiceUpdateView(UpdateView):
template_name = 'home/invoice_form.html'
model = invoice
form_class = InvoiceForm
def get_success_url(self):
self.success_url = 'home/invoice_form.html'
return self.success_url
def get_context_data(self, **kwargs):
context = super(InvoiceUpdateView, self).get_context_data(**kwargs)
if self.request.POST:
context['form'] = InvoiceForm(self.request.POST, instance=self.object)
context['invdescription_form'] = InvoiceFormSet(self.request.POST,
instance=self.object)
else:
context['form'] = InvoiceForm(instance=self.object)
context['invdescription_form'] = InvoiceFormSet(instance=self.object)
return context
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form_class = self.get_form_class()
form = self.get_form(form_class)
qs = invoice_description.objects.filter(invoice=self.get_object())
invdescription_form = InvoiceFormSet(self.request.POST, queryset=qs)
if (form.is_valid() and invdescription_form.is_valid()):
return self.form_valid(form, invdescription_form)
else:
return self.form_invalid(form, invdescription_form)
def form_valid(self, form, invdescription_form, *args, **kwargs):
form.save()
invdescription_form.instance = self.object
invdescription_form.save()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form, invdescription_form):
return self.render_to_response(
self.get_context_data(form=form,
invdescription_form=invdescription_form))
我想更新表单集中所做的更改而不删除它。