Django Querying Datetimes Incorrectly?
Basically I have two datetimes, one from each model. According to python, the datetimes are equal. When I try to query one type of object with the other's datetime, no results are returned. However when I try to do the reverse and query the other by the first's datetime, a result is returned. The examples below should clarify what is wrong.
My models look like this (abbreviated):
class Shift(models.Model):
normalized_start = models.DateTimeField(null=True)
class ScheduledShift(models.Model):
start_datetime = models.DateTimeField()
The unexpec开发者_StackOverflowted behavior of not returning a result:
>>> shift = m.Shift.objects.get(pk=796)
>>> scheduled_shift = m.ScheduledShift.objects.get(pk=1)
>>> shift.normalized_start == scheduled_shift.start_datetime
True
>>> shift.normalized_start, scheduled_shift.start_datetime
(datetime.datetime(2011, 1, 4, 23, 15), datetime.datetime(2011, 1, 4, 23, 15))
>>> m.ScheduledShift.objects.filter(start_datetime=shift.normalized_start)
[]
However, when I query the other model (this is what should have happened in the last example)...
>>> m.Shift.objects.filter(normalized_start=scheduled_shift.start_datetime)
[<Shift 796>]
I am using SQLite if it makes any difference.
Try looking at the SQL generating the Tables themselves. Based on the comments on your question, It sounds like SQLite is using different datatypes to store the null vs. non-null data, and this is causing a formatting error.
精彩评论