views:

92

answers:

1

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?

+1  A: 

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.

Alexander Ljungberg
Thank you alexander, thats really useful. I'm very new to Python but using that example it looks like I can have a lot of fun with .count() - Thank you again!
Tristan
One extra question, using this same methodology, is it possible to count objects within an object. Lets say I want to count the numbers of names in profile. Name is a field defined in the profile model.
Tristan
If the Profile has a name field then every Profile object will have a name - so counting the name fields is the same as counting the Profile objects! If you want to count Profiles with a non null name you could try, Profile.objects.filter(name__isnull=False).count()
Alexander Ljungberg