我试图在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