Muhsin
Muhsin

Reputation: 59

how do you calculate number of visits for all pages in the database

Assuming you have a Page model with a '''visits_count (IntegerField)''' field, how do you calculate the arithmetic mean of the number of visits for all pages in the database?

Upvotes: 1

Views: 592

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476557

You can aggregate over the Pages:

from django.db.models import Avg

avg_visits = Page.objects.aggregate(
    avg_visits=Avg('visits_count')
)['avg_visits']

Upvotes: 1

Related Questions