简体   繁体   中英

How can I retrieve brands based on specific category in django?

I can retrieve all product based on specific category_name and Brand name, but i can not retrieve brands name based on specific category_name . How can I do this?

My Category Model

class Category(models.Model):
    category_name = models.CharField(max_length=20, unique=True)
    logo = models.ImageField(upload_to='Category')
    slug = models.SlugField(unique="True", help_text="Slug is a field in autocomplete mode, but if you want you can modify its contents")

    def __str__(self):
        return self.category_name

My Brand Model


class Brand(models.Model):
    brand_name = models.CharField(max_length=40, null=True, blank=True, unique=True)

    def __str__(self):
        return self.brand_name

My Product Model

class Product(models.Model):
    product_name = models.CharField(max_length=50, blank=False, help_text='Add Product Model Name or Product Name.')
    category = models.ForeignKey(Category, on_delete=models.CASCADE, default="", blank=True, related_name="Products")
    brand_name = models.ForeignKey(Brand, on_delete=models.CASCADE, default="", blank=True, related_name="Products")
    specification = models.CharField(max_length=400, blank=True, default="")
    price = models.FloatField(blank=False, default=0.00, validators=[MinValueValidator(0.0)], help_text='Price Can not be Less than Zero.')
    quantity = models.PositiveBigIntegerField(default=0)

    class Meta:
        unique_together = [['category', 'brand_name', 'product_name', 'specification'] ]
    
    def __str__(self):
        return self.product_name

Based on your models it should be something like:

brand_names = Product.objects.filter(category__category_name="some_name").values_list("brand_name__brand_name", flat=True)

or if by brand_name you mean Brand model instances then

brand_names = Product.objects.filter(category__category_name="some_name").brand_name.all()

(BTW I would consider renaming fields in your models. In Product model rename brand_name to just brand and inside Brand rename field brand_name to just name . The same for Category. It will be much less confusing)

You can .filter(…) [Django-doc] with:

Brand.objects.filter()

or for a category name:

Brand.objects.filter(
    )

the .distinct() call [Django-doc] prevents returning the same Brand multiple times.


Note : normally the name of the fields in a Django model are written in snake_case , not PascalCase . Since the value for the related_name=… parameter [Django-doc] specifies the name of a field that will be defined on the target model, the value of the related_name=… parameter should be written in snake_case as well, so it should be: products instead of Products .


Note : Your Product model acts as a junction table for a many-to-many relation between Category and Brand . You can span a ManyToManyField [Django-doc] on the Category model with:

 class Category(models.Model): # … brands = models.ManyToManyField( Brand, through='Product' )

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.

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