首页 > 解决方案 > 如何在 django 中为每个类别获取唯一记录

问题描述

我有一个名为 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)

在产品表中,我将品牌表链接为品牌,将产品类型表链接为产品类型,因此假设产品表中的每个品牌都有多个记录。所以我只想要每个具有最低 product_price 的品牌的 1 条记录,并且该记录必须与 product_type 匹配。

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

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

产品id:2,产品名称:xyz,产品类型:会计,品牌:aaa,价格:50 美元

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

产品ID:4,产品名称:ghf,产品类型:其他,品牌:ccc,价格:150 美元


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

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

我需要你的帮助!

标签: mysqldjangodjango-modelsdjango-orm

解决方案


首先,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')
)

StackOverflow 上的其他相关问题:


推荐阅读