【发布时间】:2020-11-24 07:50:57
【问题描述】:
我有一张发票表
# app/models.py
class tbl_invoice(models.Model):
invoice_id = models.IntegerField(blank=True, null=True)
invoice_number = models.IntegerField(blank=True, null=True)
quotation_id = models.IntegerField(blank=True, null=True)
quotation_number = models.IntegerField(blank=True, null=True)
invoiced = models.CharField(max_length=50, default='', blank=True, null=True)
此表包含发票和报价记录,系统用户可以选择将任何报价转换为发票,但报价记录仍会与新生成的发票记录一起保留
这是我的看法
#views.py
obj = tbl_invoice.objects.get(pk='someId') # getting existing record with pk
obj.pk = None
obj.invoice_id = 'someId'
obj.quotation_id = None
obj.invoice_number = 'someValue'
obj.quotation_number = None
obj.invoiced = 'no'
obj.type_status = 'invoice'
obj.save()
上面的代码工作正常,它创建了一个新的发票记录并维护了旧的报价记录
但是,在将报价单转换为发票后,我还想将报价单记录上的invoiced 值更新为yes
为此,我尝试过
obj.update(invoiced = 'yes')
但.update() 不适用于.get()
如何从现有记录创建新记录并同时更新旧记录 还是我必须使用多个查询
感谢您的帮助。
【问题讨论】:
标签: django django-models orm