Django 统计每个年龄

标签 django orm

我有这个模型:

class User_Data(AbstractUser):
    date_of_birth = models.DateField(null=True,blank=True)
    city = models.CharField(max_length=255,default='',null=True,blank=True)
    address = models.TextField(default='',null=True,blank=True)
    gender = models.TextField(default='',null=True,blank=True)

我需要运行 django 查询来获取每个年龄的计数。像这样的事情:

Age || Count
10  ||  100
11  ||  50
and so on.....

最佳答案

这是我对 lambda 所做的事情:

usersAge = map(lambda x: calculate_age(x[0]), User_Data.objects.values_list('date_of_birth'))
    users_age_data_source = [[x, usersAge.count(x)] for x in set(usersAge)]
    users_age_data_source = sorted(users_age_data_source, key=itemgetter(0))

关于Django 统计每个年龄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45822331/

相关文章:

python - 覆盖现有的 django-admin 命令

python - 下一个查询参数不适用于 facebook 登录的 django allauth

mysql - 外键和基本关系表

python - 大量调用 SQLAlchemy 的 InstanceState 类的 expire 方法

python - Django FilteredSelectMultiple : 'TypeError while rendering: unpack non-sequence'

javascript - FOR 在 JS 脚本中循环遍历 Django 模型

asp.net-mvc - 为 "simple"SaaS 启动选择 ORM (ASP.NET MVC)

Django:基于注释进行注释

java - 如果两个实体表之间没有关系,如何连接这些表?

python - 优化 Django 中的计数查询