How to find number of users, number of users with a profile object, and monthly logins in Django

StackOverflow https://stackoverflow.com/questions/1210099

  •  06-07-2019
  •  | 
  •  

Question

Is there an easy way in Django to find the number of Users, Number of Users with profile objects, and ideally number of logins per month (but could do this with Google Analytics). I can see all the data is there in the admin interface, but I'm unsure of how to get to it in Python land. Has anyone seen any examples of counting number of user objects?

Was it helpful?

Solution

Count the number of users:

import django.contrib.auth
django.contrib.auth.models.User.objects.all().count()

You can use the same to count the number of profile objects (assuming every user has at most 1 profile), e.g. if Profile is the profile model:

Profile.objects.all().count()

To count the number of logins in a month you'd need to create a table logging each login with a time stamp. Then it's a matter of using count() again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top