Issue
Consider a table called DataTable. It has two fields, A and B. I want to return all rows of this table plus annotate a field called C that is a concatenation of A and B.
Here is what I have tried
from django.db.models import CharField, Value
from .models import DataTable
def Test(request):
query = DataTable.objects.all().annotate(C=Value('A' + '-' + 'B',
output_field=CharField()))
# the rest of the function...
problem here is that C is literally just the string literal "A - B" for every returned row. I want to use the actual values of A and B into that concatenation.
Solution
looks like you need concat:
from django.db.models import CharField, Value
from django.db.models.functions import Concat
query = DataTable.objects.annotate(C=Concat('A',
Value('-'), 'B', output_field=CharField()))
Answered By - Brown Bear
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.