Question about m2m and forms

50 views
Skip to first unread message

Carlo Ascani

unread,
Feb 17, 2017, 1:24:50 PM2/17/17
to django...@googlegroups.com
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

Matthew Pava

unread,
Feb 17, 2017, 2:09:15 PM2/17/17
to django...@googlegroups.com
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')))
--
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...@googlegroups.com.
To post to this group, send email to django...@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.

Carlo Ascani

unread,
Feb 18, 2017, 5:02:25 AM2/18/17
to Django users, Matthe...@iss.com


Il giorno venerdì 17 febbraio 2017 20:09:15 UTC+1, Matthew Pava ha scritto:
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')))


 Thank you!
That works really well.


Carlo Ascani

unread,
Apr 2, 2017, 12:52:52 PM4/2/17
to Django users, Matthe...@iss.com
The next step to this is would be to put the count in a separate property, so I can render it independently in the template (in a span, for example)
is that possible ?

Thank you in advance
Carlo
 
 
Reply all
Reply to author
Forward
0 new messages