简体   繁体   English

将Python中的月份和年份与日期时间匹配

[英]Matching Month and Year In Python with datetime

I'm working on a blog using Django and I'm trying to use get() to retrieve the post from my database with a certain post_title and post_date. 我正在使用Django开发博客,并且尝试使用get()从数据库中检索具有特定post_title和post_date的帖子。 I'm using datetime for the post_date, and although I can use post_date = date(year, month, day) to fetch a post made on that specific day, I don't know how to get it to ignore the day parameter, I can't only pass two arguments to date() and since only integers can be used I don't think there's any kind of wildcard I can use for the day. 我使用datetime作为post_date,尽管我可以使用post_date = date(year,month,day)来获取在该特定日期发布的帖子,但我不知道如何使它忽略day参数,我不能仅将两个参数传递给date(),并且由于只能使用整数,因此我认为当天我不能使用任何通配符。 How would I go about doing this? 我将如何去做呢?

To clarify, I'm trying to find a post using the year in which it was posted, the month, and it's title, but not the day. 为了明确起见,我正在尝试使用发布的年份,月份和标题(而不是日期)来查找帖子。 Thanks in advance for any help! 在此先感谢您的帮助!

您可以使用post_date__yearpost_date__month

What you're looking for is probably coverd by post_date__year=year and post_date__month=month in django. django中的post_date__year=yearpost_date__month=month可能涵盖了您要查找的内容。

Nevertheless all this seems a little bit werid for get parameters. 尽管如此,所有这些似乎对于get参数来说有点令人头疼。 Do you have any constraint at database level that forbids you from putting there two posts with the same title in the same month of given year? 您在数据库级别是否有任何约束,禁止您在给定年份的同一个月内放置两个标题相同的帖子?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM