[英]Django - How to calculate age of 10 years above
我有一个正常的 Model:
date_of_birth = models.DateTimeField(blank=True, null=True)
请帮我计算10年及以上的并显示在html中。 我需要设置它,以便在今天有人满 10 岁时,它会自动显示它们。
使用relativedelta
从当前时间减去 10 年,然后在查询集中使用它:
from django.utils import timezone
from dateutil.relativedelta import relativedelta
ten_years_ago_time = timezone.now() - relativedelta(years=10)
result = YourModel.objects.filter(date_of_birth__lt=ten_years_ago_time)
这段代码应该views.py
中,与models.py
无关
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.