python - Add count value to set -


i'm using django-taggit tag items in todo list app.

i'm trying list each of tags along number of actions associated each tag may read:

tag (1) tag b (3) tag c (2) 

tag has 1 item, tag b has 3, etc.

i added boolean field django-taggit. i'm getting list of tags this:

visible_tags = tag.objects.filter(visible=true).order_by('name') hidden_tags = tag.objects.filter(visible=false).order_by('name') 

i can count of items (actions) this:

for tag in visible_tags:     print tag     print action.objects.filter(tags__name__in=[tag]).count() 

now want attach these counts visible_tags , hidden_tags set can iterate on them in template this:

{% tag in visible_tags %}     {{ tag }} ({{ tag.count }})<br> {% endfor %} 

how can attach .count value each tag within visible_tags , within hidden_tags? assume have iterate on tags in each set?

use annotations: https://docs.djangoproject.com/en/dev/topics/db/aggregation/

from django.db.models import count  tag.objects.annotate(action_count=count('action')) 

(you might have tweak bit. i'm guessing on related name action)


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -