gpt4 book ai didi

mysql - django复杂查询过滤并创建列表

转载 作者:行者123 更新时间:2023-11-29 19:09:15 25 4
gpt4 key购买 nike

类别或型号

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

S_history 类

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_History表可能包含相同条形码的多个条目

列表看起来像

[{"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_barcode对象并使用

product_detail = S_History.objects.filter(barcode=product_barcode.barcode )

关于mysql - django复杂查询过滤并创建列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43229496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com