# оба варианта не работают
class ProductFilter(django_filters.FilterSet):
def __init__(self, *args, **kwargs):
slug = kwargs.pop('slug', None)
super(ProductFilter, self).__init__(*args, **kwargs)
self.filters['brand'].extra.update({
'queryset': Brand.objects.filter(is_active=True,
product_brand__isnull=False,
product_brand__category__slug=slug),
'empty_label': '',
'help_text': False
})
# self.brand = django_filters.ModelMultipleChoiceFilter(field_name='brand__name', to_field_name='name',
# label='Бренды')
# self.brand.queryset = Brand.objects.filter(is_active=True, product_brand__isnull=False,
# product_brand__category__slug=slug)
class Meta:
model = Product
fields = ['brand']
class ProductFilter(django_filters.FilterSet):
brand = django_filters.ModelMultipleChoiceFilter(field_name='brand__name',
to_field_name='name',
label='Бренды',
queryset=Brand.objects.filter(is_active=True,
product_brand__isnull=False,
product_brand__category__slug='slug_category'),
widget=forms.CheckboxSelectMultiple)
class Meta:
model = Product
fields = ['brand']
class ProductFilter(django_filters.FilterSet):
brand = django_filters.ModelMultipleChoiceFilter(field_name='brand__name',
to_field_name='name',
label='Бренды',
queryset=Brand.objects.all()
)
def __init__(self, *args, **kwargs):
slug = kwargs.pop('slug', None)
super(ProductFilter, self).__init__(*args, **kwargs)
self.filters['brand'].queryset = Brand.objects.filter(is_active=True,
product_brand__isnull=False,
product_brand__category__slug=slug),
widget=forms.CheckboxSelectMultiple)
class Meta:
model = Product
fields = ['brand']