I want to literally add a string to a queryset object. Why, because I'm sending this to JSON and it would be so nice and clean to just put the information there and have it available without having to iterate over the queryset to turn it into a custom dictionary.
What I have now:
a_vote_set.aggregate(
count = Count('id'),
avg=Avg('score'),
std=StdDev('score'),
sum=Sum('score'),
)
Which gets me this:
{"count": 1, "std": 0.0, "sum": -4.0, "avg": -4.0}
What I want to get is:
{"count": 1, "std": 0.0, "sum": -4.0, "avg": -4.0, "additional_value": "name of candidate"}
Which I would love to get by calling something like this:
a_vote_set.aggregate(
count = Count('id'),
avg=Avg('score'),
std=StdDev('score'),
sum=Sum('score'),
additional_value=Literal(candidate.name),
)
or this:
a_vote_set.aggregate(
count = Count('id'),
avg=Avg('score'),
std=StdDev('score'),
sum=Sum('score')
).append(
additional_value=str(candidate.name),
)
Any ideas on if this is possible?
You can add literal value with Value:
from django.db import models
a_vote_set.aggregate(
count = Count('id'),
avg=Avg('score'),
std=StdDev('score'),
sum=Sum('score')
).annotate(
additional_value=models.Value(candidate.name, output_field=models.CharField()),
)
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