Home > front end >  Is it possible for Django to sum lengths of a non-numeric field?
Is it possible for Django to sum lengths of a non-numeric field?

Time:11-02

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')))

CodePudding user response:

from django.db.models.functions import Length
from django.db.models import Sum
User.objects.annotate(l=Length("username")).aggregate(Sum("l"))

CodePudding user response:

you can try use

from django.db.models.functions import Length User.objects.aggregate(Sum(Length('username')))

  • Related