Opened 4 years ago
Last modified 4 years ago
#32059 closed Bug
Issue when counting auth_user extended model by month — at Initial Version
Reported by: | anderleich | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 2.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I've been debugging for I while but I can't get the right result. In my django application I have extended the auth_user to add more fields:
from django.contrib.auth.models import User class Person(User): api_id = models.CharField(max_length=255) api_key = models.CharField(max_length=255)
Now, I want to count persons by year and month according to the date they joined (date_joined
from auth_user
):
Person.objects.annotate(month=TruncMonth("date_joined")).values("month").annotate(count=Count("pk"))
However, all I get is a count=1 for each person in the database without grouping them by month.
This is the actual query being executed:
SELECT CAST(DATE_FORMAT(CONVERT_TZ(`auth_user`.`date_joined`, 'UTC', 'Europe/Madrid'), '%Y-%m-01 00:00:00') AS DATETIME) AS `month`, COUNT(`mt_api_app_person`.`user_ptr_id`) AS `count` FROM `mt_api_app_person` INNER JOIN `auth_user` ON (`mt_api_app_person`.`user_ptr_id` = `auth_user`.`id`) GROUP BY CAST(DATE_FORMAT(CONVERT_TZ(`auth_user`.`date_joined`, 'UTC', 'Europe/Madrid'), '%Y-%m-01 00:00:00') AS DATETIME), `auth_user`.`date_joined` ORDER BY `auth_user`.`date_joined` DESC
All count=1 seem to occur because of the GROUP BY [...] 'auth_user'.'date_joined'
which is grouping the entries by the whole datetime and not just month and year.
Any clues of what's happening here? If I do the same query but with the User I get what I want.