Just a quick note to sock away my code snippet, and hopefully help others. When working on Django development, the SQLite backend is awesome; it’s simple, self-contained, and easy to carry about (I carry apps on my USB fob). But be careful with datatypes; datetimes in particular (at least for me).

SQLite (v2) stores everything as text under the covers, even though is accepts typed column declaration (it keeps track of the datatype you declare, but doesn’t do anything with it really. v3 adds some typing possibilites, but I don’t think Django leverages this. Consider everything a string.). So you can insert an invalid datetime fragment via Django code (this is most common when you’re calculating it yourself, rather than using the form widgets), and you may not realize anything is awry until you try to pull data back out. You’ll start to get unpack errors and such, as the Django SQLite datetime parsing code fails. After a bit of munging about, I tracked down out the incantation to take a 9-tuple time struct and pop it properly into a SQLite datetimefield:

if item.has_key('date'):
    foo = item.get('date_parsed') # date_parsed is a 9-tuple 
    pc_date = datetime.datetime(*foo[:6]) # this generates the correct input type

Hopefully I can save someone a bit of pain. *grin*

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.