Answer the question
In order to leave comments, you need to log in
django_filters. How to make a foreign key in the form of a set of Checkboxes?
I need to replace this view:
With this:
Models
class Product(models.Model):
"""Store product"""
category = models.ForeignKey(
'Category',
on_delete=models.SET_NULL,
null=True,
related_name='products'
)
class Category(models.Model):
"""Product category"""
name = models.CharField(max_length=50)
class ProductFilter(django_filters.FilterSet):
category = django_filters.ModelMultipleChoiceFilter(queryset=Category.objects.all(), widget=forms.CheckboxSelectMultiple)
class Meta:
model = Product
fields = ['category']
class ProductFilterView(ListView):
model = Product
template_name = 'store/product-filter.html'
context_object_name = 'products'
def get_context_data(self, **kwargs):
context['filter'] = ProductFilter(self.request.GET, queryset=self.get_queryset())
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