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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CABVJJr8E47NCdbg%3DL0LwD%2BShMjFZ%3DD7zMsLgGCVxAj0Zjx4Mfw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to