我有一個關於一次保存三個表單的問題。前兩種形式保存沒有問題,但第三種形式必須在form2保存到數據庫時從form2請求id。不知道這個程序的問題在哪裏,我犯了什麼錯誤。Django ORM一次保存多個表單
models.pyclass Product_service(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255, blank=True, null=True)
selling_price = models.DecimalField(decimal_places=5, max_digits=255, blank=True, null=True)
purchase_price = models.DecimalField(decimal_places=5, max_digits=255, blank=True, null=True)
description = models.CharField(max_length=255, blank=True, null=True)
image = models.FileField(upload_to="/", blank=True, null=True)
product_code = models.CharField(max_length=255, blank=True, null=True)
product_code_supplier = models.CharField(max_length=255, blank=True, null=True)
product_code_buyer = models.CharField(max_length=255, blank=True, null=True)
min_unit_state = models.CharField(max_length=255, blank=True, null=True)
state = models.CharField(max_length=255, blank=True, null=True)
vat_id = models.ForeignKey('VatRate', related_name='vat_rate')
unit_id = models.ForeignKey('Units', related_name='unit_value')
category_id = models.ForeignKey('Category', related_name='product_services', blank=True, null=True)
def __str__(self):
return self.name
該模型代表窗口2。
class InvoiceProdService(models.Model):
id = models.AutoField(primary_key=True)
invoice_id = models.ForeignKey('OutgoingInvoice', related_name='outgoing_invoice', blank=True, null=True)
prod_serv_id = models.ForeignKey('Product_service', related_name='product_services', blank=True, null=True)
code = models.IntegerField(blank=True, null=True)
prod_serv_name = models.CharField(max_length=255,blank=True, null=True)
description = models.CharField(max_length=255,blank=True, null=True)
rate_name = models.CharField(max_length=255,blank=True, null=True)
units = models.CharField(max_length=255, blank=True, null=True)
price = models.DecimalField(decimal_places=5, max_digits=255, blank=True, null=True)
vat = models.DecimalField(decimal_places=5, max_digits=255, blank=True, null=True)
current_vat = models.DecimalField(decimal_places=5, max_digits=255, blank=True, null=True)
price_no_vat = models.DecimalField(decimal_places=5, max_digits=255, blank=True, null=True)
def __str__(self):
return self.id
該模型表示form3。 其中兩個字段與ForeignKey關聯到form2。
invoice_id = models.ForeignKey('OutgoingInvoice', related_name='outgoing_invoice', blank=True, null=True)
prod_serv_id = models.ForeignKey('Product_service', related_name='product_services', blank=True, null=True)
當我與後臺管理界面嘗試,我可以直接保存表單到數據庫中。它工作得很好。
views.py
這裏是保存方法的邏輯:
if request.method == 'POST':
form1 = InsertNewCustomer(request.POST, prefix='form1')
form2 = Outdrew(request.POST, prefix='form2')
form5 = MultiplyInsertValues(request.POST, prefix='form5')
if (form1.is_valid() and form2.is_valid() and form5.is_valid()):
a, created = OrganizationInfo.objects.get_or_create(**form1.cleaned_data)
if created:
b = form2.save(commit=False)
b.user_id = user_pk
b.organization_id = org_id.id
b.customer_id = a
b.save()
c = form5.save(commit=False)
nesto = Product_service.objects.get(name= form5.cleaned_data['prod_serv_name'])
c.prod_serv = nesto.id
c.save()
return HttpResponseRedirect('/izlazni_racuni/')
else:
b = form2.save(commit=False)
organ_id = OrganizationInfo.objects.get(oib=form1.cleaned_data['oib'])
b.user_id = user_pk
b.organization_id = org_id.id
b.customer_id = organ_id.id
b.save()
c = form5.save(commit=False)
nesto = Product_service.objects.get(name= form5.cleaned_data['prod_serv_name'])
c.invoice_id = b.id
c.prod_serv_id = nesto.id
c.save()
return HttpResponseRedirect('/izlazni_racuni/')
else:
form1 = InsertNewCustomer(prefix='form1')
form2 = Outdrew(prefix='form2')
form5 = MultiplyInsertValues(prefix='form5')
在此之後,我得到錯誤信息: 不能分配 「80」: 「InvoiceProdService.invoice_id」 必須是一個 「OutgoingInvoice」實例。
而且問題是行:c.invoice_id = b.id
Django的版本:1.7.7
不知道哪裏是在這個問題上的問題。
Thx for solution,@spectras – marin