I have an SQLite database with a timestamp column defined as a DateTime field. It contains values like 2014-10-14T14:51:07.558
My Django model has the following field:
timestamp = models.DateTimeField(db_column='timestamp', blank=False)
Then, when I run the following code, I get an empty set as a result:
Model.objects.filter(timestamp=Model.objects.all()[0].timestamp)
or
edit: removed a 'contains' call
Querying
Model.objects.all()[0].timestamp
returns
datetime.datetime(2014, 10, 14, 14, 51, 7, 558000, tzinfo=<UTC>)
Why can't I filter?
Datetimes are not strings; a datetime is a single object, so it can't 'contain' anything other than itself exactly. According to the django docs though , sqlite doesn't have a native datetime type, and django uses strings instead, but in python datetimes are not strings.
The docs aren't clear about what should happen filtering datetime fields using __contains, but it doesn't make much sense to me to do so. If you are trying to check whether a specific datetime falls within a range, use __range .
That said, you tried a straight = filter, and it didn't have any effect. Try filtering against str(<datetime>)
since it will be a string in sqlite.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.