Django - filtering of objects by parameters
Th开发者_StackOverflowere is a catalog of products, the products have properties ... want to filter products by the properties.
class Product(models.Model):
name = models.CharField(verbose_name="Название",max_length=255)
description = models.TextField(verbose_name="Описание")
category = models.ManyToManyField(Category,verbose_name=("Категория"))
class Product_Attribute(models.Model):
product = models.ForeignKey(Product)
option = models.ForeignKey(Attribute_Option)
value = models.CharField(verbose_name=("Value"), max_length=255)
class Attribute_Option(models.Model):
description = models.CharField(verbose_name=("Description"), max_length=100)
name = models.SlugField(verbose_name=("Attribute name"), max_length=100)
I want to filter for two or more attribute
Your original question is extremely ambiguous as to what properties you wish to filter the Product objects on. If you want all Product objects that relate to a specific Attribute_Option.description value, you could use:
attribute_options = Attribute_Option.objects.get(description='foo')
product_attributes = Product_Attribute.objects.select_related('Product').filter(option__in=attribute_options)
results = [p.product for p in product_attributes]
If you simply want all Product objects that have both a specific name and a specific description, you could use:
Product.objects.filter(name='foo', description='bar')
I really have no idea which properties you are trying to filter on though. If you specify that in your question, you might be able to get an answer that matches your specific use-case.
精彩评论