Issue
I have learned we can sum all(or filtered) columns like price
from this question.
ItemPrice.objects.aggregate(Sum('price'))
But is it possible for Django to sum non-numeric field's length, such as CharField
or JSONField
? A pseudocode is like following.
User.objects.aggregate(Sum(len('username')))
Solution
from django.db.models.functions import Length
from django.db.models import Sum
User.objects.annotate(l=Length("username")).aggregate(Sum("l"))
Answered By - ramwin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.