5

I have a query that retrieve an object like this:

{
    "id": 1,
    "tags": [1, 2, 3]
}

I want to check whether a given tag (say, 1) exists in the tags field of the object, and annotate the result of the check as a field of the object:

{
    "id": 1,
    "tagged": true
}

This is what I came up with

annotate(
    tagged=Exists(
        Articles.tag_set.through.objects.filter(
            article_id=OuterRef("pk"), tag_id=tag.id
        )
    )
)

Since the relation tags is already loaded by the primary query, having a secondary query seems redundant to me.

Is there an easier way to structure this query? Something close to the filter in lookup syntax.

jc127
  • 343
  • 2
  • 13

1 Answers1

3

Assuming tags is an ArrayField

    q = Article.objects.annotate(
        tagged=Exists(
            Article.objects.filter(id=OuterRef('id'), tags__contains=[1])
        )
    )
Aprimus
  • 1,463
  • 1
  • 14
  • 12