G
G
Grigory Dikiy2017-08-11 09:18:29
Django
Grigory Dikiy, 2017-08-11 09:18:29

Django ORM filter?

Good afternoon! Can't create query in django. The filters on the site are made in the following way:

class FilterCategory(CreationModificationDateMixin):
    category = TreeManyToManyField(
        'category.Category',
        verbose_name='Категория фильтра'
    )

    name = models.CharField(
        'Название фильтра для категории',
        max_length=250
    )

    slug = models.SlugField(
        'Название фильтра для URL',
        max_length=250
    )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'Фильр'
        verbose_name_plural = 'Фильтры'


class FilterSelect(CreationModificationDateMixin):
    filter_category = models.ForeignKey(
        FilterCategory,
        verbose_name='Фильтр Категории',
        related_name='values'
    )

    name = models.CharField(
        'Значение фильтра',
        max_length=250
    )

    slug = models.SlugField(
        'Значение фильтра для URL',
        max_length=250
    )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'Занчение фильтра'
        verbose_name_plural = 'Значение фильтров'


class ProductFilter(models.Model):
    product = models.ForeignKey(
        'products.Product',
        on_delete=models.CASCADE,
        verbose_name='Товар',
        related_name='filters'
    )

    filter_category = models.ForeignKey(
        FilterCategory,
        null=True,
        on_delete=models.CASCADE,
        verbose_name='Фильтр категории'
    )

    values = models.ForeignKey(
        FilterSelect,
        null=True,
        blank=True,
        verbose_name='Значения фильтра'
    )

    def __str__(self):
        return 'Фильтр'

    class Meta:
        verbose_name = 'Фильтр товаров'
        verbose_name_plural = 'Фильтры товаров'

That is, the filter is bound to specific categories, FilterSelect is bound to FilterCategory, and ProductFilter is bound to the product and both FilterSelect and FilterCategory.
In the template, I display the values ​​like this:
{% for fil in product.filters.all %}
 <div class="col-md-6">
   <div class="row">
    <div class="col-xs-6"><div class="sec-t">{{ fil.filter_category.name }}</div></div>
    <div class="col-xs-6">
        <div class="sec-v"><a href="#">{{ fil.values.name }}</a></div>
    </div>

</div>
{% endfor %}

But in the category itself I can not figure out how to filter products. Tried this
for value in values:
    qs.filter(filters__values__slug=value)

Where value is the slug for the filter that comes from the frontend. This option does not work. And the idea is simple: filter products for which slug values ​​in FilterSelect are defined exactly the same as in the values ​​array.
How can this be done?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question