I have this value 1/19/2014 4:40
in my database from my model, does the DateTimeField accept that value in Django? Because I'm trying to filter my model based on range. Currently, it is CharField
, I will change it to DateTimeField
because I guess it is impossible to filter range by CharField
. Am I right?
Asked
Active
Viewed 284 times
0

ferrangb
- 2,012
- 2
- 20
- 34

Sachi Tekina
- 1,800
- 5
- 28
- 50
1 Answers
0
For the range filtering, the field must be stored in the SQL database in a format SQL range queries can be performed.
Char field is just text and thus SQL cannot know what kind of ranges you query, as it doesn't understand the text.
Thus you need to convert your field to DateTimeField. Most likely this kind of migration cannot be performed with the default setup and you need to write some of your own migration logic. You can use helper package like dateutil.
Create a new field in your model
Migrate datababase
Parse all old values and store in the new field
Delete the old field
Rename the new field to the old field

Mikko Ohtamaa
- 82,057
- 50
- 264
- 435
-
So, does my current value is accepted in Django `DateTimeField`? Because my data is auto-generated from a running program. – Sachi Tekina Mar 25 '15 at 07:16
-
Please see the edit - my browser crashed in the middle of comment – Mikko Ohtamaa Mar 25 '15 at 07:17
-
Actually, I can directly edit my value from my other program, so that the next time it will generate it is on the proper format. What I do not know is, the `DateTimeField` values that are accepted or the proper format of the said field. – Sachi Tekina Mar 25 '15 at 07:32
-
DateTimeField does not accept strings, but datetime objects. https://docs.python.org/2/library/datetime.html - you need to parse your datetime strings to datetime objects first. – Mikko Ohtamaa Mar 25 '15 at 08:07
-
Yep, I would follow the steps above. To parse the strings to datetime check this http://stackoverflow.com/questions/466345/converting-string-into-datetime – brunofitas Mar 25 '15 at 11:44