【问题标题】:django query datetime filed issuedjango查询日期时间字段问题
【发布时间】:2018-08-06 03:51:39
【问题描述】:

我尝试在 django 查询中应用过滤日期时间,但结果为零

class Customers(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30, null=True)
    customer_code = models.CharField(max_length=30)
    reference = models.CharField(max_length=30, null=True)
    phone_office = models.CharField(max_length=250, null=True)
    phone_residential = models.CharField(max_length=250, null=True)
    contact_person_first_name = models.CharField(max_length=30)
    mobile = models.BigIntegerField()
    email = models.EmailField(null=True)
    fax = models.CharField(max_length=15, null=True)
    cr_limit = models.DecimalField(max_digits=10, decimal_places=2, null=True)
    gstin = models.CharField(max_length=10, null=True)
    state_code = models.CharField(max_length=10, null=True)
    country = models.CharField(max_length=250, null=True)
    opening_balance = models.DecimalField(max_digits=10, decimal_places=2, null=True)
    opening_balance_date = models.DateTimeField(null=True)
    payments_terms = models.ForeignKey(PaymentTerms, related_name='customer_terms', null=True, blank=True)
    address_flag = models.BooleanField(default=False)
    created_by = models.ForeignKey(SAUser, related_name='+')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

我尝试过的以下代码:

Customers.objects.filter(created_at__month=1).count()

但我也看过查询

Customers.objects.filter(created_at__month=1).query

好像是这样

SELECT `customer_customers`.`id`, `customer_customers`.`first_name`, `customer_customers`.`last_name`, `customer_customers`.`customer_code`, `customer_customers`.`reference`, `customer_customers`.`phone_office`, `customer_customers`.`phone_residential`, `customer_customers`.`contact_person_first_name`, `customer_customers`.`mobile`, `customer_customers`.`email`, `customer_customers`.`fax`, `customer_customers`.`cr_limit`, `customer_customers`.`gstin`, `customer_customers`.`state_code`, `customer_customers`.`country`, `customer_customers`.`opening_balance`, `customer_customers`.`opening_balance_date`, `customer_customers`.`payments_terms_id`, `customer_customers`.`address_flag`, `customer_customers`.`created_by_id`, `customer_customers`.`created_at`, `customer_customers`.`updated_at` FROM `customer_customers` WHERE EXTRACT(MONTH FROM CONVERT_TZ(`customer_customers`.`created_at`, 'UTC', IST)) = 1

mysql中的手动查询

select id,created_at from customer_customers;
+----+----------------------------+
| id | created_at                 |
+----+----------------------------+
|  1 | 2017-12-24 06:54:41.264756 |
|  2 | 2017-12-24 07:05:37.317395 |
|  3 | 2017-12-24 10:05:29.957158 |
|  4 | 2017-12-29 13:30:21.572926 |
|  5 | 2017-12-29 13:58:59.137774 |
|  6 | 2017-12-31 08:46:13.239080 |
|  7 | 2017-12-31 09:04:34.695830 |
|  8 | 2017-12-31 12:27:05.253016 |
|  9 | 2018-01-27 12:28:16.809840 |
| 10 | 2018-02-14 07:27:18.847884 |
| 11 | 2018-02-14 10:45:33.323448 

预期结果应该是 2

【问题讨论】:

  • 在此处粘贴您的models.py
  • @AstikAnand models.py 添加了
  • 您的查询似乎正确。
  • @AstikAnand 你明白我的问题吗
  • Customers.objects.filter(created_at__month=1).count() 这是对的。它应该返回 2。尝试使用其他字段进行过滤。

标签: django django-models django-rest-framework django-orm


【解决方案1】:

"当 USE_TZ 为 True 时,日期时间字段被转换为当前 过滤前的时区。这需要在 数据库。”

将此添加到您的设置文件中。

USE_TZ = 假

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 2012-09-10
    • 2014-06-29
    • 2016-07-09
    • 2012-10-14
    相关资源
    最近更新 更多