I need too annotate the result of a regex lookup in a When() conditional expression using the then parameter, DB is Postgresql.
For example you can use the SubStr() function to get a substring of a field, I want to be able to use a regular expression instead of a positional substring.
I was thinking some custom function using 'REGEXP_MATCHES', I'm not sure how I'd do this though, or if it's even necessary.
qs = SomeModel.objects.all()
pattern = r'regex'
qs = qs.annotate(
match=Case(
When(
some_model_field__regex=pattern,
then=Regex('some_model_field', pattern)
),
output_field=CharField()
)
)
I want the annotation to be the result of the pattern match.