Get objects for which a foreign key type exists

I use django-taggit to create an application that stores not only tasks, but also information elements. You can specify certain elements and information elements.

When I pull out a list of tags for tasks, I run the following query:

action_tags = Tag.objects.order_by('name').filter(action__complete=False).annotate(action_count=Count('action'))

This gives me the name of all tags for which there is incomplete data. It also gives me a partial dose count.

For information elements, there is no field for "complete"; information elements are simply "there." Therefore, I want to write a query that pulls out all the tags for which there is at least one information element. How can this be written?

+5
source share
1 answer

, , , info_item , , :

tags = Tag.objects.filter(
    info_item__isnull=False
).order_by(
    'name'
).annotate(
    info_item_count=Count('info_item')
)
+6

All Articles