简体   繁体   English

如何在 Django 中获取每个类别的唯一记录

[英]How to Get Unique Record for each category in django

I have a table called product_type & Product.我有一个名为 product_type & Product 的表。

class Product_type(models.Model):
    product_type_id = models.AutoField(primary_key=True)
    product_type_name = models.CharField(max_length=255, null = False, unique=True)
    product_type_title = models.CharField(max_length=255, null = True)
    product_type_description = models.TextField(null = False)
    product_type_slug = models.SlugField(unique=True, blank=True, null=True)
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, related_name='category')
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

class Product(models.Model):
    product_id = models.AutoField(primary_key=True)
    product_name = models.CharField(max_length=255, null = False, unique=True)
    product_slug = models.SlugField(unique=True, blank=True, null=True)
    product_title = models.CharField(max_length=255, null = True)
    product_info = models.TextField(null = False)
    product_description = models.CharField(max_length = 255)
    product_price = models.CharField(max_length = 255)
    brand = models.ForeignKey(Brand, on_delete=models.SET_NULL, null=True)
    product_type = models.ForeignKey(Product_type, on_delete=models.SET_NULL, null=True)
    product_status = models.CharField(max_length = 100, default = "publish")
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

class Brand(models.Model):
    brand_id = models.AutoField(primary_key=True)
    brand_name = models.CharField(max_length=255, null = False, unique=True)
    brand_slug = models.SlugField(unique=True, blank=True, null=True)
    brand_title = models.CharField(max_length = 255)
    brand_logo = models.ImageField(upload_to = 'brand/logo/%Y/%m/')
    brand_logo_alt_text = models.CharField(max_length=255, null = False)
    brand_info = models.TextField(null = False)
    brand_description = models.CharField(max_length = 255)
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
    country = models.ForeignKey(Country, default = '1', on_delete=models.SET_DEFAULT, null=True)
    brand_status = models.CharField(max_length = 100, default = "publish")
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

in product table I've linked brand table as brand and product_type table as product_type, so suppose I've multiple records for each brand in product table.在产品表中,我将品牌表链接为品牌,将 product_type 表链接为 product_type,因此假设我在产品表中为每个品牌有多条记录。 So I only want 1 record for each brand which has a lowest product_price and that record must match with product_type.所以我只想要每个具有最低 product_price 的品牌的记录,并且该记录必须与 product_type 匹配。

for example: I want only those product which contains product_type = 'accounting'.例如:我只想要那些包含 product_type = 'accounting' 的产品。 so here is a demo data: product(model data)所以这是一个演示数据:产品(模型数据)

product_id:1,product_name:abc, product_type: accounting, brand:aaa, price:$100 product_id:1,product_name:abc,product_type:accounting,brand:aaa, price:$100

product_id:2,product_name:xyz, product_type: accounting, brand:aaa, price:$50 product_id:2,product_name:xyz,product_type:accounting,brand:aaa, price:$50

product_id:3,product_name:bdf, product_type: accounting, brand:bbb, price:$150 product_id:3,product_name:bdf,product_type:accounting,brand:bbb, price:$150

product_id:4,product_name:ghf, product_type: other, brand:ccc, price:$150 product_id:4,product_name:ghf,product_type:其他,品牌:ccc,价格:150 美元


so my query result will be product_id 2,3 because 1,2 have same brand but 2 has the lowest price & 2,3 both have product_type = accounting.所以我的查询结果将是 product_id 2,3,因为 1,2 具有相同的品牌,但 2 具有最低价格 & 2,3 都具有 product_type = 会计。

I tried too much but nothing works.我尝试了太多,但没有任何效果。

I want your help!我需要你的帮助!

First, use models.DecimalField for product prices.首先,对产品价格使用models.DecimalField Do not use models.CharField for numbers.不要将models.CharField用于数字。 Change the product_price field to product_price = models.DecimalField(max_digits=6, decimal_places=2) (Stores $0 to $9999.99).product_price字段更改为product_price = models.DecimalField(max_digits=6, decimal_places=2) (存储 $0 到 $9999.99)。

Then, you can try the following:然后,您可以尝试以下操作:

from django.db.models import F, Min

Product.objects.filter(
    product_type=my_product_type
).annotate(
    min_brand_price=Min('brand__product_type__product_price')
).filter(
    product_price=F('min_brand_price')
)

Other related questions on StackOverflow: StackOverflow 上的其他相关问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM