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.
Assuming tags is an ArrayField
q = Article.objects.annotate(
tagged=Exists(
Article.objects.filter(id=OuterRef('id'), tags__contains=[1])
)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With