如何从 django python 的查询集对象中的日期时间字段获取月份和年份(2020 年 1 月)?
how to get month and year (Jan, 2020) from datetimefield in queryset object in django python?
我有一个 table,其中包含字段 ID、Img、UploadDate 和更多字段。
这就是 models.py
的样子
class ImgDetails(models.Model):
Img = models.ImageField(upload_to='media')
Category = models.ForeignKey(CategoryList, default=1, on_delete=models.SET_DEFAULT, null=False)
keywords = models.CharField(max_length=255)
User = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
Valid = models.BooleanField(default=False)
UploadDate = models.DateTimeField(auto_now_add=True)
在 views.py
中,我想获取 id、Img 和 UploadDate,但是在 UploadDate 字段(属于 datetimefield 类型)中我只想获取月份和年份,我该如何获取它。
我试过:
collectMonthYear = ImgDetails.objects.filter(User_id=user.id,Valid=True).annotate(Date=('UploadDate').strftime("%m-%Y")).values('id','Img','Date')
此引发错误:'str' object has no attribute 'strftime'
谁能帮我解决这个问题。我怎样才能使用只有月份和年份的 'UploadDate' 字段获取数据。
如果您想继续使用相同的逻辑,可以使用 date() 函数将 str 更改为 date。
您可以检查 来完成它。但是,您可以在从 queryset
获得结果后进行格式化,如下所示:
queryset = ImgDetails.objects.filter(User_id=user.id,Valid=True)
for result in queryset:
dt = result.UploadDate.strftime("%m-%Y")
您想采用哪种方式取决于您的用例。
您可以使用 django 内置的数据库函数。
from django.db.models.functions import ExtractMonth, ExtractYear
ImgDetails.objects.filter(User_id=user.id,Valid=True).annotate(month=ExtractMonth('UploadDate'),year=ExtractYear('UploadDate'))
关注此 link 了解更多详情。
感谢大家的帮助。
我这样做是为了实现它 - 我首先在查询集中获取值
queryset = ImgDetails.objects.filter(User_id=user.id,Valid=True).annotate(month=ExtractMonth('UploadDate'),year=ExtractYear('UploadDate'))
然后通过在单个键中连接月份和年份将其转换为字典列表
queryset_value = []
print(queryset)
for result in queryset:
queryset_value.append({'id': result.id, 'Img': result.Img.url, 'UploadDate' : str(result.month)+"-"+str(result.year)})
print(queryset_value)
输出:
[{'id': 24, 'Img': '/media/1-cover_bg_2.jpg', 'UploadDate': '7-2020'}, {'id': 26, 'Img': '/media/1-person_woman_1.jpg', 'UploadDate': '7-2020'}, {'id': 27, 'Img': '/
media/1-person_woman_3.jpg', 'UploadDate': '7-2020'}, {'id': 29, 'Img': '/media/1-person_man_2.jpg', 'UploadDate': '7-2020'}, {'id': 30, 'Img': '/media/1-person_man_
3.jpg', 'UploadDate': '7-2020'}, {'id': 31, 'Img': '/media/1-person_woman_2.jpg', 'UploadDate': '7-2020'}]
我有一个 table,其中包含字段 ID、Img、UploadDate 和更多字段。
这就是 models.py
的样子
class ImgDetails(models.Model):
Img = models.ImageField(upload_to='media')
Category = models.ForeignKey(CategoryList, default=1, on_delete=models.SET_DEFAULT, null=False)
keywords = models.CharField(max_length=255)
User = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
Valid = models.BooleanField(default=False)
UploadDate = models.DateTimeField(auto_now_add=True)
在 views.py
中,我想获取 id、Img 和 UploadDate,但是在 UploadDate 字段(属于 datetimefield 类型)中我只想获取月份和年份,我该如何获取它。
我试过:
collectMonthYear = ImgDetails.objects.filter(User_id=user.id,Valid=True).annotate(Date=('UploadDate').strftime("%m-%Y")).values('id','Img','Date')
此引发错误:'str' object has no attribute 'strftime'
谁能帮我解决这个问题。我怎样才能使用只有月份和年份的 'UploadDate' 字段获取数据。
如果您想继续使用相同的逻辑,可以使用 date() 函数将 str 更改为 date。
您可以检查 queryset
获得结果后进行格式化,如下所示:
queryset = ImgDetails.objects.filter(User_id=user.id,Valid=True)
for result in queryset:
dt = result.UploadDate.strftime("%m-%Y")
您想采用哪种方式取决于您的用例。
您可以使用 django 内置的数据库函数。
from django.db.models.functions import ExtractMonth, ExtractYear
ImgDetails.objects.filter(User_id=user.id,Valid=True).annotate(month=ExtractMonth('UploadDate'),year=ExtractYear('UploadDate'))
关注此 link 了解更多详情。
感谢大家的帮助。
我这样做是为了实现它 - 我首先在查询集中获取值
queryset = ImgDetails.objects.filter(User_id=user.id,Valid=True).annotate(month=ExtractMonth('UploadDate'),year=ExtractYear('UploadDate'))
然后通过在单个键中连接月份和年份将其转换为字典列表
queryset_value = []
print(queryset)
for result in queryset:
queryset_value.append({'id': result.id, 'Img': result.Img.url, 'UploadDate' : str(result.month)+"-"+str(result.year)})
print(queryset_value)
输出:
[{'id': 24, 'Img': '/media/1-cover_bg_2.jpg', 'UploadDate': '7-2020'}, {'id': 26, 'Img': '/media/1-person_woman_1.jpg', 'UploadDate': '7-2020'}, {'id': 27, 'Img': '/
media/1-person_woman_3.jpg', 'UploadDate': '7-2020'}, {'id': 29, 'Img': '/media/1-person_man_2.jpg', 'UploadDate': '7-2020'}, {'id': 30, 'Img': '/media/1-person_man_
3.jpg', 'UploadDate': '7-2020'}, {'id': 31, 'Img': '/media/1-person_woman_2.jpg', 'UploadDate': '7-2020'}]