【发布时间】:2022-01-16 06:32:33
【问题描述】:
我有一个发票系统,员工或员工可以在其中创建发票,并可以为特定客户添加多个产品和数量。当我使用 mysql 时,我无法获取 json 数据或数组数据。所以我将数量和价格(折扣和其他修改后)作为字符串,然后在显示或打印发票时,我使用正则表达式查找数量和价格.我在一个manytomay字段中添加了产品ID,我从中获取产品名称和售价。在我使用 zip 时在打印页面上显示数据时,产品显示为产品的 ID,因此我想以保存数据的方式检索数据。或者你能告诉我有什么更简单的方法吗? 这是我的 models.py
class Invoice(models.Model):
customers = models.ForeignKey(Customer, on_delete=models.CASCADE)
products = models.ManyToManyField(Product)
total = models.FloatField()
date = models.DateField(auto_now_add=True)
amounts = models.CharField(max_length=500, default="00")
quantity = models.CharField(max_length=500, blank=True)
def save(self, *args, **kwargs):
if not Invoice.objects.count():
self.id = 20210001
else:
self.id = Invoice.objects.last().id + 1
super(Invoice, self).save(*args, **kwargs)
这是我的打印页面功能的views.py
def final_billing(request, id=None):
pk = id
obj = Invoice.objects.get(id=pk)
products = obj.products.all()
customer = Customer.objects.get(id=obj.customers.id)
amn = obj.amounts
qt = obj.quantity
list_of_comma = re.findall("[\d+.+\d]+", amn)
amnts = [float(n) for n in list_of_comma]
list_of_quantity = re.findall('[0-9]+', qt)
qty = [int(n) for n in list_of_quantity if n.isdigit()]
products = list(products)
both = zip(products,amnts,qty)
return render(request, 'finalbill.html',{'bills': obj, "due": customer, "both": both})
我希望按照保存的顺序检索产品对象
【问题讨论】:
标签: python-3.x django django-views