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

Pay attention, that visits_count is Integer Field

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

Pay attention, that visits_count is Integer Field

If youâ€™re talking about doing it within a query, Cast the visit_counts to a FloatField before dividing by the number of rows.

1 Like

I can`t understand what do you mean by dividing by the number of rows. Can I get a code? Sorry for the stupid questions, but I`

m a newbie in this.Thanks in advance Sir

Whatâ€™s the definition of â€śthe arithmetic meanâ€ť?

My bad

*Arithmetical mean*

In other word - average

Yes, I understand that. But what is the defintion of an `average`

? Or, to phrase it differently, how would you calculate the average of a set of numbers?

the number of users visited per page

Mathematically, what is the formula for calculating an average?

avg=(a1+a2+a3+a4+â€¦+an)/n

Ok, so what does â€śnâ€ť represent in *this* specific case? And how do you think it relates to your earlier question?

If I`m not wrong it means â€śvisitsâ€ť

No, thatâ€™s not quite correct.

Letâ€™s say I have 5 numbers:

2, 6, 8, 14, 20

What is the average of these 5 numbers?

What then is the â€śnâ€ť in your formula?

1.(2+6+8+14+20)/5 = 10 is the average of these numbers

2. amount of pages

Hope I replied correctly this time

Correct! So, in your formula, youâ€™re creating the sum of a set of numbers, and then dividing that by the number of numbers in that set.

In the case of the problem youâ€™re trying to solve, the â€śnumber of numbers in the setâ€ť is the number of different pages for which you have a â€śvisit_countâ€ť, or, in other words, the number of rows (or number of â€śentriesâ€ť if you prefer) in your Page model.

And *that* is what I mean by:

yeah, that makes sense right now

thanks for the calmness and for working with me

1 Like