Hi all,
I have a model A with a m2m to B:
------------------------------------------
class A(models.Model):
to_b = models.ManyToManyField(B)
class B(models.Model):
name = models.CharField(max_length=100)
------------------------------------------
In a view, I am listing A objects and I am building
a form to use B as a filter.
------------------------------------------
class AFilterForm(forms.Form):
to_b = forms.ModelMultipleChoiceField(
widget=forms.CheckboxSelectMultiple(),
queryset = B.objects.all())
------------------------------------------
This is the view:
------------------------------------------
class AListViewWithFilter(ListView):
model = A
def get_context_data(self, **kwargs):
context = super(AListViewWithFilter, self).get_context_data(**kwargs)
qs = A.objects.all()
b_filter = None
if self.request.GET:
if 'to_b' in self.request.GET:
b_filter = self.request.GET.getlist('to_b')
res = res.filter(to_b__in=b_filter).distinct()
context.update({
'results': qs,
'form': AFilterForm(initial={
'to_b': b_filter
})
return context
------------------------------------------
Everything is working fine.
I would like to add a count of how many A objects are there
for any of the to_b. So that the filter form would look like this:
[ ] to_b_value_1 (34)
[ ] to_b_value_2 (12)
[ ] to_b_value_3 (3)
...
Is that possible?
Thank you in advance
Best,
Carlo
--
Carlo Ascani aka carloratm
Frontend Developer
www.carlorat.me
carloratm@freenode