Django 通过日期的天来筛选模型

huangapple go评论62阅读模式
英文:

Django Filter model by Day of Date

问题

I am at a loss. What I'm doing is very simple and works in dev but not prod.

我感到困惑。我在开发环境中的操作非常简单,但在生产环境中不起作用。

I have a model, EmailLog, which contains a DateTimeField with a default of timezone.now. Model is created in database with the correct timestamp.

我有一个模型,EmailLog,其中包含一个带有默认值timezone.now的DateTimeField。模型在数据库中创建时具有正确的时间戳。

I would like to display any models that were created today.

我想显示今天创建的任何模型。

Model Snippet

模型片段

class EmailLog(models.Model):
     created_on = models.DateTimeField(default=timezone.now)
     ...

Model shows in database as 2023-05-10 16:31:34.381 for created_on and a raw print(log.created_on) shows 2023-05-10 16:31:34.381741+00:00

模型在数据库中显示为2023-05-10 16:31:34.381,created_on的原始打印结果为2023-05-10 16:31:34.381741+00:00

I'm attempting to pull logs for today like so

我尝试像这样提取今天的日志

logs = list(EmailLog.objects.filter(created_on__day=10, created_on__month=5))

This works perfectly fine in dev, in production I'm given no results but also no errors.

这在开发环境中完全正常工作,在生产环境中我没有结果,也没有错误。

I've tried created_on__date=timezone.now().date(), created_on__gte=timezone.now().date(), also tried various forms of the date in string ISO format 2023-05-10, I've tried created_on__range=(start_date,end_date) where start_date is 2023-05-10 00:00:00 and end_date is 2023-05-10 23:59:59 and I get nothing.

我尝试过created_on__date=timezone.now().date()created_on__gte=timezone.now().date(),还尝试过各种形式的ISO格式日期2023-05-10,我还尝试过created_on__range=(start_date,end_date),其中start_date是2023-05-10 00:00:00,end_date是2023-05-10 23:59:59,但我什么都没有得到。

Both dev and prod database are MySQL, but quite possibly different versions as the production app is in Azure using an Azure MySQL. That is the only difference I can see between app deployments, both are using a virtual env, both configured with some packages, all managed with poetry and identical.

开发和生产数据库都是MySQL,但很可能是不同的版本,因为生产应用程序在Azure上使用Azure MySQL。这是我在应用部署之间唯一能看到的区别,两者都使用虚拟环境,都配置了一些包,都由poetry管理,并且是相同的。

I'm at a loss, please point out the bonehead mistake I'm making.

我感到困惑,请指出我犯的愚蠢错误。

英文:

I am at a loss. What I'm doing is very simple and works in dev but not prod.

I have a model, EmailLog, which contains a DateTimeField with a default of timezone.now. Model is created in database with the correct timestamp.

I would like to display any models that were created today.

Model Snippet

class EmailLog(models.Model):
     created_on = models.DateTimeField(default=timezone.now)
     ...

Model shows in database as 2023-05-10 16:31:34.381 for created_on and a raw print(log.created_on) shows 2023-05-10 16:31:34.381741+00:00

I'm attempting to pull logs for today like so

logs = list(EmailLog.objects.filter(created_on__day=10, created_on__month=5))

This works perfectly fine in dev, in production I'm given no results but also no errors.

I've tried created_on__date=timezone.now().date(), created_on__gte=timezone.now().date(), also tried various forms of the date in string ISO format 2023-05-10, I've tried created_on__range=(start_date,end_date) where start_date is 2023-05-10 00:00:00 and end_date is 2023-05-10 23:59:59 and I get nothing.

Both dev and prod database are MySQL, but quite possibly different versions as the production app is in Azure using an Azure MySQL. That is the only difference I can see between app deployments, both are using a virtual env, both configured with some packages, all managed with poetry and identical.

I'm at a loss, please point out the bonehead mistake I'm making.

答案1

得分: 1

以下是已翻译的内容:

正如此文章所解释的,MySQL使用(否则会使用什么)表来存储时区以及如何进行转换。

由于某种原因,这个表可能被截断,因此转换到不同时区会失败。令人有点沮丧的是,数据库会返回NULL,而不会引发错误。

无论如何,系统通常都有一些工具来列出时区。在Unix/Linux系统中,例如,这些数据包含在大多数软件包管理器的tz-info包中。然后,文件存储在/usr/share/zoneinfo目录中,以指定这些时区的定义方式。

Azure MySQL数据库具有一个存储过程,可以检查系统上的时区,然后用关于这些时区的信息填充相应的表。您可以在MySQL会话中执行以下操作:

mysql> CALL mysql.az_load_timezone();

您甚至可能希望偶尔运行此命令,以保持时区的同步。偶尔某些时区的具体信息会发生变化(轻微)。MySQL将不会查找存储在系统中的数据的转换,而是查找其表中的数据。

英文:

As explained in this article, MySQL uses (well what would it use otherwise) a table to store the timezones and how these are converted.

For some reason, this table might be truncated, and thus the conversion to a different timezone fails. It is a bit sad that the database then returns NULL, and does not raise some error.

Anyway, a system usually has some tools to list the timezones. In Unix/Linux systems for example, that data is contained in the tz-info package of most package managers. Files are then stored in the /usr/share/zoneinfo directory to specify how these timezones are defined.

Azure MySQL databases have a stored procedure that can inspect the timezones on the system, and then fill the corresponding tables with information regarding these timezones. You can do this in a MySQL session with:

mysql> CALL mysql.az_load_timezone();

You might even want to run this command every now and then, to let the timezones keep in sync. Every now and then the specifics of some timezones change (slightly). MySQL will not look for the conversion to the data stored in the system, but in its tables.

huangapple
  • 本文由 发表于 2023年5月11日 02:32:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定