G
G
Grigory Dikiy2017-07-28 10:02:46
Django
Grigory Dikiy, 2017-07-28 10:02:46

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 = 'Атрибуты продукта'

It's simple, a certain category has certain attributes, and the value itself is filled in for a certain product. The question is how do I filter out products, for example, which contain a certain value (size: 12 cm) and so on. I understand that it was possible to create something like a predefined choice, but this approach is unprofitable when the spread of values ​​​​is too large.
What is currently implemented:
urls.py
url(r'^attribute/(?P<id>[-\w]+)-(?P<category_id>[-\w]+)/$', views.AttributeProducts.as_view(), name='filter'),

views.py
...
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)

Everything works and filters, but there is a problem from the SEO side. Since for each product you need to set an attribute, it turns out that the attribute: 2222-11 and 21221-11 (for example) can be the same, respectively, there can be a lot of unnecessary links in the index.
What options do I see:
1) Close these links from indexing and leave them so that the system works
2) Write your own slugify type converter and decoder for it, so the value is passed through the url
Are there any other solutions to this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NaName, 2017-07-28
@NaName

Look at this link, maybe there will be something useful (like gte, lte) https://docs.djangoproject.com/en/1.11/ref/models/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question