Mysql django复杂查询筛选并创建列表

Mysql django复杂查询筛选并创建列表,mysql,django,python-2.7,django-models,Mysql,Django,Python 2.7,Django Models,类或模型 class Category(models.Model): category_name = models.CharField(max_length=50) company_name = models.CharField(max_length=50) timestamp = models.DateTimeField(auto_now_add = True, auto_now = False) updated = models.DateTimeField(

类或模型

class Category(models.Model):

    category_name = models.CharField(max_length=50)
    company_name = models.CharField(max_length=50)
    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.category_name


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)


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)


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)
1> 第一项任务 查找产品名称、成本、标志??使用产品的条形码值\u条形码表

高中历史

class S_History(models.Model):
    barcode = models.CharField(max_length=500)
    email = models.CharField(max_length=100)
    timestamp = models.DateTimeField(auto_now_add = True, auto_now = False)
2> 如果S_History.barcode=prodcut_barcode.barcode,则创建列表

S_历史记录表可能包含相同条形码的多个条目

列表看起来像

[{"product_name":"fff","cost":"999"},{"product_name":"xxxwww","cost":"55"}]
1) 您的第一个查询如下所示

 product_detail = product_barcode.objects.filter().values('flag', 'ProductDetails__cost', 'ProductDetails__product__product_name')
2) 对于第二个查询,您需要产品\条形码的对象 和使用


提供异常类型对象“product\u barcode”没有“barcode”属性检查product\u barcode表是否包含条形码字段
product_detail = S_History.objects.filter(barcode=product_barcode.barcode )