Il giorno sabato 18 febbraio 2017 11:02:25 UTC+1, Carlo Ascani ha scritto:
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.
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
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+unsubscribe@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/71f50624-64aa-45b2-9ae8-7428d8e846b5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment