I have a table called 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. So I only want 1 record for each brand which has a lowest product_price and that record must match with product_type.
for example: I want only those product which contains 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: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:4,product_name:ghf, product_type: other, brand:ccc, price:$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.
I tried too much but nothing works.
I want your help!
First, use models.DecimalField
for product prices. Do not use models.CharField
for numbers. Change the product_price
field to product_price = models.DecimalField(max_digits=6, decimal_places=2)
(Stores $0 to $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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.