DateTimeField 查询集在 Django 中返回 None

2023-10-08数据库问题
13

本文介绍了DateTimeField 查询集在 Django 中返回 None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试创建一个查询集,用于获取数据库中 DATETIME 的 DateTimeField 的值.

I am trying to create a queryset for getting the values of a DateTimeField which is DATETIME in the DB.

models.py 中的类:

The class in models.py:

class ChangeMetrics(models.Model):
    id = models.IntegerField(primary_key=True)
    file_id = models.ForeignKey(File, db_column = 'file_id')
    version_id = models.ForeignKey(Version, db_column = 'version_id')
    function_id = models.ForeignKey(Function, blank=True, db_column = 'function_id')
    date = models.DateTimeField(blank=True, null=True)
    user = models.TextField(blank=True)
    changed = models.IntegerField(blank=True, null=True)

数据库中的字段:

date DATETIME

元组填充在数据库中,直接在数据库上运行 SQL 查询运行良好.

The tuples are populated in the database and running SQL queries directly on the DB is working perfectly.

这是我目前在 Django 中使用的查询集:

This is the queryset I am currently using in Django:

queryset = ChangeMetrics.objects.filter(~Q(changed=None), ~Q(date=None), ~Q(version_id=None))

我尝试了一个原始查询和一个使用 exclude() 的查询版本,但仍然返回 None 日期.

I have tried a raw query and also a version of the query that uses exclude(), but that still returns None for date.

我正在通过 for 循环访问查询集中的条目,并通过 for 循环内的 entry.date 简单地访问日期.

I am accessing the entries in the queryset through a for loop and simply accessing date through entry.date inside the for loop.

Django 1.6.5 版我也尝试通过 Django shell 获取值,但没有成功.

Django version 1.6.5 I have also tried getting the values through the Django shell, to no success.

有什么可能出问题的想法吗?

Any ideas on what could be wrong?

推荐答案

不确定你是否能够解决这个问题,但我刚刚遇到了这个问题并且能够解决它.

not sure if you were able to figure this out, but I just had this problem and was able to fix it.

因此,Django 迁移将 DB 中的列创建为 datetime(6) 而不是 datetime.这导致 Django 模型无法实例化 Datetime 对象.

So, Django migrations were creating the column in the DB as datetime(6) instead of datetime. This was causing the Django models to fail to instantiate the Datetime object.

ALTER TABLE `my_table` 
MODIFY COLUMN `created` datetime NOT NULL

运行后,如果解决了我的问题.也许在您的情况下,您可以尝试修改为 datetime(6) .

After running that if fixed my issue. Maybe in your case you could try modifying to datetime(6) instead.

这篇关于DateTimeField 查询集在 Django 中返回 None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Django MySQLdb 版本与 _mysql 版本 Ubuntu 不匹配
Django MySQLdb version doesn#39;t match _mysql version Ubuntu(Django MySQLdb 版本与 _mysql 版本 Ubuntu 不匹配)...
2024-04-16 数据库问题
11

如何使用Python+SQLAlchemy远程连接MySQL数据库?
How to connect MySQL database using Python+SQLAlchemy remotely?(如何使用Python+SQLAlchemy远程连接MySQL数据库?)...
2024-04-16 数据库问题
15

在 SQLite 中按月分组
Group by month in SQLite(在 SQLite 中按月分组)...
2024-04-16 数据库问题
6

Python 中参数查询的语法 (pyodbc)
Syntax for a parameter query in Python (pyodbc)(Python 中参数查询的语法 (pyodbc))...
2024-04-15 数据库问题
11

Django - 向 MySQL 数据库添加行
Django - Add rows to MySQL database(Django - 向 MySQL 数据库添加行)...
2024-04-15 数据库问题
2

如何从 Python 中的数据库创建 CSV 文件?
How do I create a CSV file from database in Python?(如何从 Python 中的数据库创建 CSV 文件?)...
2024-04-15 数据库问题
3