【发布时间】:2017-03-29 20:12:36
【问题描述】:
Django 模型
class Product(models.Model):
product_name = models.CharField(max_length=50)
category = models.CharField(max_length=50)
desc = models.CharField(max_length=120)
company_name = models.CharField(max_length=50, default=None)
timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
updated = models.DateTimeField(auto_now_add = False, auto_now = True)
def __unicode__(self):
return self.product_name
class ProductDetails(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
batch = models.CharField(max_length=50)
quantity = models.IntegerField()
cost = models.FloatField(null=True, blank=True, default=None)
mfg = models.DateTimeField()
exp = models.DateTimeField()
timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
updated = models.DateTimeField(auto_now_add = False, auto_now = True)
def __unicode__(self):
return self.batch
class product_barcode(models.Model):
batch = models.ForeignKey(ProductDetails, on_delete=models.CASCADE)
barcode = models.BigIntegerField()
flag = models.IntegerField()
timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
updated = models.DateTimeField(auto_now_add = False, auto_now = True)
def __unicode__(self):
return self.barcode
我想计算由 company_name 生成的条形码数量
我尝试了一些类似的代码
w=Product.objects.all().filter(company_name="company name")
print "w>>>",w
pid = []
for Pdata in w:
pid.append(Pdata.id)
print "pid>>>",pid
在这里我找到属于特定公司的产品名称 ID,但之后我想找到属于产品 ID 的批次代码(在 ProductDetails),之后我需要计算所有属于批次代码的条形码。
简单来说
- 公司['公司名称']
- 产品 ['product1','product2','product3'] where company = '公司名称'
-
产品的批次代码 = ['product1','product2','product3']
- product1 ['bxyz','b2xyz','b3xyz']
- product2 ['b1xyz','b4xyz','b3xyz']
- product3 ['b9xyz','b6xyz','b3xyz']
count Barcodes where batch codes ['bxyz','b2xyz','b3xyz']['b1xyz','b4xyz','b3xyz']['b9xyz','b6xyz','b3xyz']
【问题讨论】:
标签: python django list django-models