简体   繁体   English

如何计算年龄是否在出生年份的范围内,同时从 Django ORM 中的 Db 获取出生年份

[英]How to calculate if age is in range from the birth year ,while fetching the birth year from Db in Django ORM

I have a model which consists of id, name and birth year.I want to query this model to get the name such that the age is in between a range.我有一个由 id、姓名和出生年份组成的模型。我想查询这个模型以获取年龄在一个范围内的名字。

For now, what i have tried is现在,我尝试过的是

queryset= Employees.objects.all()

For each name i calculate the age by:对于每个名字,我计算年龄:

now = datetime.datetime.now()
age=now.year-q.birth_year
ids=[]
if 25<age<36 :
   ids.append(q.id)

and then, i query once again with ids Employees.objects.filter(id__in=ids)然后,我再次查询 ids Employees.objects.filter(id__in=ids)

Can i do all this in a single query.我可以在一个查询中完成所有这些吗?

Employees.objects.filter(**Calculate age and compare if it is in the range) 

Range can be dynamic.范围可以是动态的。 I have the range in two variable minAge and maxAge.我有两个变量 minAge 和 maxAge 的范围。

Any help.任何帮助。

You might want to use relativedelta from dateutil , it's more convenient to calculate the time:你可能想使用relativedeltadateutil ,它更方便地计算时间:

import datetime
from dateutil.relativedelta import relativedelta

today = datetime.date.today()
age_25 = (today - relativedelta(years=25)).year
age_36 = (today - relativedelta(years=36)).year
Employees.objects.filter(birth_year__lte=age_25, birth_year__gte=36)

age_25 is 25 years ago, age_36 is 36 years ago, you just query the people's birthdays fall between 25 and 36 years ago. age_25是 25 年前, age_36是 36 年前,你只是查询人们的生日在 25 到 36 年前。

For lte and gte check django doc for details.对于ltegte请查看django doc以了解详细信息。

Edit :编辑

Actually, django orm supports range query, so just do:实际上,django orm 支持range查询,所以只需执行以下操作:

Employees.objects.filter(birth_year__range=[age_36, age_25])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Django ORM 计算从出生年份到注册日期的用户年龄 - How to calculate user age from birth year till registration date using Django ORM 根据年龄查找出生年份,Python 3 - Finding birth year from age, Python 3 如何在出生日期的日期字段中提取年份并在 django views.py 中计算人的年龄? - how to extract year in date field of date of birth and calculate age of person in django views.py? 使用 python 从出生日期和特定日期计算日、月、年的年龄 - Calculate age in day, month and year from birth date and a specific day using python 出生年份和实际年份的法定年龄证明人 - Legal age certifier with birth year and actual year 如何从熊猫中的出生日期计算年龄,Python(Jupyter Notebook) - How to Calculate Age from Date Of Birth in pandas, Python(Jupyter Notebook) 使用python通过计算当前年份-出生年份来创建年龄计算器 - Creating an age calculator using python by calculating current year - birth year 当熊猫的某些行之间有NaN时,如何从出生日期计算年龄? - How to calculate the age from date of birth when there are NaN in between some of the rows in pandas? 如何将出生年份的熊猫数据框列转换为年龄? (例如&#39;1991&#39;-&gt; 28) - How can I convert a pandas dataframe column with birth year to age? (e.g. '1991' -> 28) 在Excel电子表格中从出生日期查找年龄 - Finding age from Date of Birth in an excel spreadsheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM