diff --git a/beat/web/common/utils.py b/beat/web/common/utils.py index ffe47af7a51827ed18677942c00bb15331c66c90..127378d8e8847addbfc065a2ff4c8ca748263073 100644 --- a/beat/web/common/utils.py +++ b/beat/web/common/utils.py @@ -31,6 +31,8 @@ Reusable help functions from django.core.exceptions import ValidationError from django.utils.encoding import force_text from django.utils import six +from django.db.models import CharField, Value as V +from django.db.models.functions import Concat from docutils import utils from docutils.nodes import Element @@ -145,3 +147,21 @@ def py3_cmp(a, b): https://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons """ return (a > b) - (a < b) + + +def annotate_full_name(db_class): + """ + Annotate a query with the asset full name so that it can be more easily + filtered. + """ + + return db_class.objects.annotate( + full_name=Concat( + "author__username", + V("/"), + "name", + V("/"), + "version", + output_field=CharField(), + ) + )