我想从(2021-8-3)中得到一个像"8“这样的特定日期,但是它显示的像这个图像
如何提取特定日期?
usertime = User.objects.filter(groups__name = 'patient').values('date_joined').annotate(date_only=Cast('date_joined', DateField()))发布于 2021-08-11 11:16:12
from django.db.models import F, Func,Value, CharField
usertime = (User.objects.filter(groups__name = 'patient').values('date_joined')
.annotate(date_only=Func(
F('date_joined'),
Value('MM'),
function='to_char',
output_field=CharField()
)
).values('date_only'))试试这个,从@Yannics的答案中得到一个参考:https://stackoverflow.com/a/60924664/5804947
您还可以在YYYY / DD字段下分别使用Value /date,并且在使用PostgreSQL数据库时可以正常工作。
另一种方法
from django.db.models.functions import Extract
usertime = User.objects.filter(groups__name = 'patient').values('date_joined').annotate(date_only=Extract('date_joined', 'month'))https://stackoverflow.com/questions/68740616
复制相似问题