Answer the question
In order to leave comments, you need to log in
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 = 'Фильтры товаров'
{% 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 %}
for value in values:
qs.filter(filters__values__slug=value)
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