Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
65 views
in Technique[技术] by (71.8m points)

python - Django SUM Query with VIEW Statement

I am trying to sum query whole table using Django Query. I have an also Postgresql database working with my django.

My Table workoutsummaryview: (actually its VIEW statements):

user_id workout results
1 Bench 281.31
1 Flat 137.06
1 Bench 1497.91
1 Bench 64.50
1 SQ 555.65
1 Wrench 2803.39
1 Flat 2115.73
1 Bench 10578.63
1 Flat 146.52
1 Flat 1770.65
1 Wrench 22.61
question from:https://stackoverflow.com/questions/65882272/django-sum-query-with-view-statement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As was mentioned in comments, probably aggregation results are empty. If that's a case, you can handle this situation by doing this:

Dailyw.objects.aggregate(Sum('results'))['results__sum'] or 0

, then in the template you'll see a value (zero) instead of black space.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...