Reputation: 7910
I've been using Django's ORM annotate
before with success for a few times, but I'm having trouble with this especific case.
I want to annotate a new field, called tag_to_show
, when the value of the field my_tag
matches a certain regular expression.
This is what I have for the moment:
queryset.annotate(tag_to_show=Case(When(my_tag__iregex=pattern,
then=Value("I don't know what to put here")),
output_field=CharField(),
default=Value("Not matched")))
I'm just applying a regular expression to the my_tag
field. If the regex matches the string contained in the my_tag
field of a certain object, I want to annotate its value in a new field called tag_to_show
.
Any ideas what to put inside de Value
parameter?
Upvotes: 0
Views: 2431
Reputation: 19831
I think what you want is the F() expression
:
queryset.annotate(tag_to_show=Case(
When(my_tag__iregex=pattern, then=F('my_tag')),
output_field=CharField(),
default=Value("Not matched")))
Upvotes: 2
Reputation: 3100
I think the answer is:
queryset.annotate(tag_to_show=Case(When(my_tag__iregex=pattern,
then='my_tag'),
output_field=CharField(),
default=Value("Not matched")))
Upvotes: 0