comments = project.masarif.filter(active=True)
def sub_masarif():
for sub in comments:
total = 0
xdd = sub.count
total = (xdd)
print(total) # 3 4 69
i need sum total
< example print(total) # 76
CodePudding user response:
You each time reset total
, hence it each time prints the current value. You should set the total = 0
variable outside the loop, so:
def sub_masarif():
total = 0
for sub in project.masarif.filter(active=True):
xdd = sub.count
total = sub.count
print(total)
But you here aggregate at the Django/Python layer. Normally it is better to do this at the database side, so:
from django.db.models import Sum
def sub_masarif():
total = project.masarif.filter(active=True).aggregate(total=Sum('count'))['total']
print(total)