【问题标题】:How to Get Unique Record for each category in django如何在 django 中为每个类别获取唯一记录
【发布时间】:2020-05-29 23:25:21
【问题描述】:

我有一个名为 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:accounting,brand:aaa, price:$100

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

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

product_id:4,product_name:ghf, product_type: other, brand:ccc, price: $150


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

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

我需要你的帮助!

【问题讨论】:

  • 请解释你的意思:“....每个brand_id只有1条记录,价格最低”。给我们使用样本数据的例子。同时向我们展示您的尝试。
  • 我的意思是查看我已将品牌表链接为品牌 ID 的产品表,所以假设我在产品表中为每个品牌有多个记录,所以我只希望每个品牌 ID 有 1 条记录,该记录具有最小值product_price 并且该记录必须与 product_type 匹配。
  • 您可能对aggregation functions感兴趣。
  • 如果存在两个具有相同品牌、产品类型和相同价格的产品恰好是最低价,您期望的行为是什么?
  • Flux 请检查问题,我已经正确更新了

标签: mysql django django-models django-orm


【解决方案1】:

首先,使用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 上的其他相关问题:

【讨论】:

    猜你喜欢
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多