0

I have a column formatted as such in one of my models:

TEMP_START = models.DateTimeField(null=True)

And I am attempting to do an exact lookup using queryset syntax such as

x.filter(TEMP_START=my_datetime_object) # x can be thought of as y.objects.all()

This returns no objects, when it should do more than one. However,

x.filter(TEMP_START__date=my_datetime_object.date()).filter(TEMP_START__hour=my_datetime_object.hour) 

Does return the proper objects (they're hourly). Are direct datetime filters not supported, and thus keywords must be used?

====== Edit with bad results:

Searching for: {'TEMP_START': datetime.datetime(2016, 3, 31, 2, 0)}

Values in column: [{'TEMP_START': datetime.datetime(2016, 3, 29, 8, 0)}, {'TEMP_START': datetime.datetime(2016, 3, 29, 14, 0)}, {'TEMP_START': datetime.datetime(2016, 3, 30, 2, 0)}, {'TEMP_START': datetime.datetime(2016, 3, 29, 20, 0)}, {'TEMP_START': datetime.datetime(2016, 3, 30, 8, 0)}, {'TEMP_START': datetime.datetime(2016, 3, 30, 20, 0)}, {'TEMP_START': datetime.datetime(2016, 3, 31, 2, 0)}, {'TEMP_START': datetime.datetime(2016, 3, 30, 14, 0)}]

Values being returned: []


Code:
    args_timeframe_start = {variables.temp_start: self.ranked_timeframes[rank][variables.temp_start]}
    print(args_timeframe_start)
    print(self.query_data.values(variables.temp_start).distinct())
    query_data = self.query_data.filter(**args_timeframe_start)
    print(query_data.values(variables.temp_start).distinct())
um8ra
  • 185
  • 2
  • 12
  • Please show exactly what you're doing, rather than using filler content like `my_datetime_object`. It'll be more helpful for us to see how you're actually doing things, and where variables are coming from. – rnevius Apr 01 '16 at 12:57
  • Do `TEMP_START` and `my_datetime_object` have exact same values? – v1k45 Apr 01 '16 at 12:57
  • Also note that "exact same" should include hours, minutes, seconds, milliseconds and timezone... – Ludwik Trammer Apr 01 '16 at 12:59
  • @LudwikTrammer, they are "exactly the same". No timezone assigned to anything. Exactly same y/m/d h:m:s, TZ (none). – um8ra Apr 01 '16 at 13:26
  • @rnevius: Please see my comment under the Shang's answer. They are coming from the same column and I am looking up the nth one (ranking). – um8ra Apr 01 '16 at 13:43

1 Answers1

1

You need to find out what is my_datetime_object but most likely because DateTime fields contain python datetime.datetime objects, datetime.datetime objects is composed of year, month, date, hour, minute, second, microsecond. So if you merely compare date and hour, sure you could get results, but you couldn't guarantee that my_datetime_object matches one of the records in your database that has the same minute, second and microsecond.

Try this quickly and you could see what does datetime look like, also django doc about DateTimeField:

from datetime import datetime
date = datetime.now()
print date
Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • All other fields are zero, and don't have a timezone set (on either object) – um8ra Apr 01 '16 at 13:24
  • How did you get `my_datetime_object`? – Shang Wang Apr 01 '16 at 13:31
  • TEMP_START = models.DateTimeField(null=True). Essentially, I'm ranking the datetimes and looking up the n-th object based on user input. So when I say the objectis in the model's field. It's guaranteed. That's where it came from. – um8ra Apr 01 '16 at 13:40
  • OK, I couldn't try it out because I don't have your environment. I tried something on my side to take a datetime from my database record field and query on that, django returns the exact result. I guess you need to debug it, try using `pdb` to examine what's `my_datetime_object` at run time. – Shang Wang Apr 01 '16 at 13:46
  • I think your filter `args_timeframe_start` is right. It should be dict that with a string of lookup criteria as key and lookup value as value. Check this SO answer to see the exact format: http://stackoverflow.com/questions/1763495/adding-extra-paramaters-to-a-objects-filter-via-dict?answertab=votes#tab-top – Shang Wang Apr 01 '16 at 14:40