Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django DateTimeField日期筛选器不工作_Python_Mysql_Django - Fatal编程技术网

Python Django DateTimeField日期筛选器不工作

Python Django DateTimeField日期筛选器不工作,python,mysql,django,Python,Mysql,Django,在我的模型中有一个名为created_at的datetime字段。我想查询今天创建的对象。从中,我使用了以下查询集 In [70]: today = datetime.datetime.today().date() In [72]: Business.objects.filter(created_at__date=today) Out[72]: <QuerySet []> 但它产生的结果带有一个警告: django/db/backends/mysql/base.py:71: War

在我的模型中有一个名为created_at的datetime字段。我想查询今天创建的对象。从中,我使用了以下查询集

In [70]: today = datetime.datetime.today().date()
In [72]: Business.objects.filter(created_at__date=today)
Out[72]: <QuerySet []>
但它产生的结果带有一个警告:

django/db/backends/mysql/base.py:71: Warning: (1292, "Incorrect datetime value: '%2017-12-21%' for column 'created_at' at row 1")
我正在使用MySQL数据库。我的模型是

class Business(models.Model):
    name = models.CharField(max_length=255, blank=True, null=True)
    url = models.CharField(max_length=255, blank=True, null=True)
    created_at = models.DateTimeField()
    category = models.CharField(max_length=120)

    class Meta:
        managed = False
        db_table = 'Business'
        unique_together = (('name', 'url'),)
使用时区库

today = timezone.datetime.today().date()
否则

我想这对你会更好

改变你的模式

created_at = models.DateTimeField(auto_now=True)

然后进行迁移,迁移并添加一些数据,然后重试this@MD.KhairulBasar:仍然没有结果。请检查此post从中创建的列数据database@Exprator:2017-05-11 04:52:25它不起作用了。即使是Business.objects.filter创建时间=datetime.date2017,12,21不生成结果。请先尝试在console中打印结果,然后在Today中签出数据检查数据类型。如果出现任何未匹配的情况,您将无法获得所需的结果ToDay为datetime.date2017,12,21,对象中的datetime字段为datetime.datetime2017,12,21,9,53,31,tzinfo=当时区支持处于活动状态时,它会显示一条警告RuntimeWarning:DateTimeField Business.created_在收到一条原始的datetime 2017-12-21 05:28:17.113688。但是没有结果。你有今天的数据吗??2017年12月21日,尝试编辑后的ans onceStill not GENT。我今天创建了一个对象。b=Business.objects.getid=4。b、 在打印datetime.datetime2017、12、21、9、53、31时创建,tzinfo=已编辑。模型是使用inspectdb命令生成的。
today = timezone.now().date()
from django.utils import timezone
Business.objects.filter(created_at__gte=timezone.now())
created_at = models.DateTimeField(auto_now=True)