1

I am sending two date values over request.GET for filtering a querySet:

if 'from_date' in request.GET:    
            from_date = request.GET['from_date']
            if 'to_date' in request.GET:    
                to_date = request.GET['to_date']
            else:
                to_date = datetime.date.today()        
            calls_queryset = calls_queryset.filter(contact_date__range=(from_date, to_date))

The filter__range breaks though. It seems it doesn't like the date format I am sending over.

?from_date=08/08/2012&to_date=08/29/2012

I think I have to cast them to be a date before placing them in range, is this correct? What is the most efficient way to do this?

Many Thanks

Houman
  • 64,245
  • 87
  • 278
  • 460

3 Answers3

3

The DateField supports converting from 'YYYY-mm-dd' formats to python datetime.date:

>>> from django.db import models
>>> models.DateField().to_python('2012-08-22')
datetime.date(2012, 8, 22)

Hence the range lookup accepts strings as parameters:

>>> from django.contrib.auth.models import User
>>> print User.objects.filter(last_login__range=('1970-01-01', '2012-12-31')).values('pk').query
SELECT "auth_user"."id" FROM "auth_user" WHERE "auth_user"."last_login" 
BETWEEN 1969-12-31 16:00:00 and 2012-12-30 16:00:00

note that last_login is a DateTimeField which tries to convert string value to datetime.datetime (and I'm in +8 timezone, hence default 00:00:00 becomes 16:00:00 one day before)

On client side, ref How do I output an ISO 8601 formatted string in JavaScript? to generate date string in the format of 'YYYY-mm-dd'.

Community
  • 1
  • 1
okm
  • 23,575
  • 5
  • 83
  • 90
  • Great answer. Thanks a lot. The ISO-8601 function looks very promising. It created a `2012-08-14T23:00:00Z` from 15/August/2010 +0 Timezone ( I think in UTC its an hour less hence 23:00 August 14th) however the range still crashes on the entries `2012-08-14T23:00:00Z`. Still investigating... – Houman Aug 29 '12 at 16:04
  • ahh just found something. Django doesn't accept ISO 8601 dates: https://code.djangoproject.com/ticket/11385 so I recon I just substring it by 10 characters? mydate = mydate[:10] ? Or do you have a better idea? :) – Houman Aug 29 '12 at 16:09
  • @Kave Django accepts `2012-08-14T23:00:00Z` now, ref [the code](https://github.com/django/django/blob/master/django/utils/dateparse.py#L25). Which version of Django do you use, and what's the result of the crash? You could use `mydate[:10]` also, the remaining bits would be filled w/ 0. – okm Sep 04 '12 at 15:20
  • I ended up doing this differently. I simply pass in the dumb data to Django and do the conversation there according to the user's time zone. It works very well, but needs the timezone, pytz implemented. This solution works too in its own way, but a bit fiddly. I accept it nonetheless. Thanks – Houman Sep 04 '12 at 19:39
1

don't use slashs in url... replace with dots or use yyymmdd format.

Davor Mlinaric
  • 1,989
  • 1
  • 19
  • 26
  • indeed yyyymmdd works. But I need to figure out a way to see which 08 is day and which one is month. i18n. I think I have to handle all that on jquery side before posting it to server. :) – Houman Aug 29 '12 at 11:59
0

Slashes is a no no, I'd convert to UNIX timestamp for convenience

Then to convert it to a python readable format you can do something like:

import time
time.ctime(int(retrieved_timestamp))
Hedde van der Heide
  • 21,841
  • 13
  • 71
  • 100
  • Thanks. I have tried it `time.ctime(int('08/08/2012'))` but it says `ValueError: invalid literal for int() with base 10: '08/08/2012'` Any idea why? – Houman Aug 29 '12 at 11:37
  • Your POST should validate a timestamp, not '08/08/2012', you could use javascript to generate real dates in your form. – Hedde van der Heide Aug 29 '12 at 11:41
  • I am actually sending this through jquery, not through the django-form. `$('#search_result').load(url)` url is created from the form input fields `?from_date=08/08/2012&to_date=08/29/2012` I think I could meddle with the format there. It just concerns me regarding US and European date format since the user can change the language/culture on the fly. – Houman Aug 29 '12 at 11:57