Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible for Django to sum non numetric field?

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 numetric field’s length, such as CharField or JSONField? A pseudocode is like following.

User.objects.aggregate(Sum(len('username')))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading