Python Django ORM一次保存多个表单

Python Django ORM一次保存多个表单,python,django,forms,orm,Python,Django,Forms,Orm,我有一个关于同时保存三张表格的问题。前两个表单保存正常,没有问题,但当form2保存到数据库时,第三个表单必须从form2请求id。不知道这个程序哪里出了问题,哪里出了错 models.py 此模型代表form2 class InvoiceProdService(models.Model): id = models.AutoField(primary_key=True) invoice_id = models.ForeignKey('OutgoingInvoice', relat

我有一个关于同时保存三张表格的问题。前两个表单保存正常,没有问题,但当form2保存到数据库时,第三个表单必须从form2请求id。不知道这个程序哪里出了问题,哪里出了错

models.py 此模型代表form2

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
这个模型代表了表3。 其中两个字段与form2的ForeignKey相关

  • invoice\u id=models.ForeignKey('OutgoingInvoice',related\u name='outgoing\u invoice',blank=True,null=True)

  • prod\u serv\u id=models.ForeignKey('Product\u service',related\u name='Product\u 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\u id”必须是“OutgoingInvoice”实例

    问题在网上:
    c.invoice\u id=b.id

    Django版本:1.7.7


    不知道这个问题出在哪里。

    它来自于您定义字段的方式

    invoice_id = models.ForeignKey('OutgoingInvoice', related_name='outgoing_invoice', blank=True, null=True)
    
    Django中的
    ForeignKey
    增加了一些魔力,因此您可以直接处理对象。也就是说,根据您的定义,
    invoice\u id
    被假定为
    OutgoingInvoice
    的实际实例,而数字id实际上是invoice\u id

    您可能应该在您的型号中将
    invoice\u id
    更改为
    invoice

    invoice = models.ForeignKey('OutgoingInvoice', related_name='outgoing_invoice', blank=True, null=True)
    
    如果执行此操作,Django会自动将
    \u id
    附加到实际数据库字段中,您可以通过id或通过以下对象访问它:

    c.invoice = b         # assign object
    c.invoice_id = b.id   # or assign id
    
    实际上,这两行在Django中几乎是等价的。
    您可以在的文档中找到更多信息。

    它来自您定义字段的方式

    invoice_id = models.ForeignKey('OutgoingInvoice', related_name='outgoing_invoice', blank=True, null=True)
    
    Django中的
    ForeignKey
    增加了一些魔力,因此您可以直接处理对象。也就是说,根据您的定义,
    invoice\u id
    被假定为
    OutgoingInvoice
    的实际实例,而数字id实际上是invoice\u id

    您可能应该在您的型号中将
    invoice\u id
    更改为
    invoice

    invoice = models.ForeignKey('OutgoingInvoice', related_name='outgoing_invoice', blank=True, null=True)
    
    如果执行此操作,Django会自动将
    \u id
    附加到实际数据库字段中,您可以通过id或通过以下对象访问它:

    c.invoice = b         # assign object
    c.invoice_id = b.id   # or assign id
    
    实际上,这两行在Django中几乎是等价的。
    您可以在以下文档中找到更多信息:.

    Thx用于解决方案,@spectrasThx用于解决方案,@spectras