Answer the question
In order to leave comments, you need to log in
Filter by Django attributes?
Good day. There are models that implement product attributes.
class CategoryAttribute(CreationModificationDateMixin):
category = TreeForeignKey(
'category.Category',
verbose_name='Категория для атрибута',
on_delete=models.CASCADE,
)
name = models.CharField(
'Название атрибута',
max_length=100
)
slug = models.SlugField(
'Название в URL'
)
class Meta:
verbose_name = 'Атрибут категории'
verbose_name_plural = 'Атрибуты категории'
def __str__(self):
return '{name}: {category}'.format(name=self.name, category=self.category.name)
class ProductAttribute(models.Model):
category_attribute = models.ForeignKey(
CategoryAttribute,
verbose_name='Атрибут категории',
on_delete=models.CASCADE,
)
product = models.ForeignKey(
'products.Product',
on_delete=models.CASCADE,
verbose_name='Товар',
related_name='attributes'
)
value = models.CharField(
'Значение атрибута',
max_length=250,
blank=True
)
def __str__(self):
return self.value
class Meta:
verbose_name = 'Атрибут продукта'
verbose_name_plural = 'Атрибуты продукта'
url(r'^attribute/(?P<id>[-\w]+)-(?P<category_id>[-\w]+)/$', views.AttributeProducts.as_view(), name='filter'),
...
self.attribute = get_object_or_404(ProductAttribute, pk=self.kwargs.get('id'))
self.category = get_object_or_404(Category, pk=self.kwargs.get('category_id'))
return Product.with_image.filter(attributes__value__icontains=self.attribute.value, category=self.category.id).order_by(sort_by)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question