Hi Carlos,
You probably want to create a new widget and override its label_from_instance 
method.

class BModelMultipleChoiceField(forms.ModelMultipleChoiceField):
        def label_from_instance(self, obj):
                return "%s (%s)" % (obj, obj.count_a)


And then you'll want to change your queryset to annotate the count so you can 
access it in the new field.
class AFilterForm(forms.Form):
        to_b = BModelMultipleChoiceField(
                widget=forms.CheckboxSelectMultiple(),
                queryset = B.objects.all().annotate(count_a=Count('a_set')))


-----Original Message-----
From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On 
Behalf Of Carlo Ascani
Sent: Friday, February 17, 2017 12:14 PM
To: django-users@googlegroups.com
Subject: Question about m2m and forms

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.

-- 
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/d4912b4e5cd0420fa1ceb6653fe8d68d%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to