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

How to Get Unique Record for each category in django

我有一个名为 product_type & Product 的 table。

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)

在产品 table 中,我将品牌 table 链接为品牌,将 product_type table 链接为 product_type,所以假设我有多个记录产品中的每个品牌 table。所以我只想为每个具有最低 product_price 的品牌记录 1 条记录,并且该记录必须与 product_type.

匹配

例如: 我只想要那些包含 product_type = 'accounting' 的产品。 所以这是一个演示数据: 产品(模型数据)

product_id:1,product_name:abc, product_type: 会计, brand:aaa, 价格:$100

product_id:2,product_name:xyz, product_type: 会计, brand:aaa, 价格:$50

product_id:3,product_name:bdf, product_type: 会计, brand:bbb, 价格:$150

product_id:4,product_name:ghf, product_type: 其他, brand:ccc, 价格:$150


所以我的查询结果将是 product_id 2,3 因为 1,2 具有相同的品牌但 2 的价格最低 & 2,3 都具有 product_type = accounting.

我尝试了太多但没有任何效果。

我需要你的帮助!

首先,使用 models.DecimalField 作为产品价格。不要使用 models.CharField 作为数字。将 product_price 字段更改为 product_price = models.DecimalField(max_digits=6, decimal_places=2)(存储 $0 到 $9999.99)。

那么,您可以尝试以下方法:

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')
)

Whosebug 上的其他相关问题: