Thread: [SQLObject] Allowing DateTime to be specified as a string...
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Ray V. D. <rva...@es...> - 2009-08-12 00:29:09
|
Hi all; It seems like this worked in the past, but I could just be imagining things :) I have a column in my database defined as a DateTimeCol with no default and where NULL is allowed. I'm parsing a CSV file and one of the columns in the CSV file corresponds with this DateTimeCol. The CSV value is a string of time.struct_time (ie parseable by strptime()). When I update a row or create a new entry passing this string as the value for the column, the validator returns an error because it's not a datetime object. I scratch my head here because I'm *sure* I have used my script in the past with similar date strings and sqlobject has transparently inserted them into my database without problem... In any case, in the here and now I'm trying to figure out the best way to handle this. I could write a simple function to check whether or not I'm on a column in my CSV file that has a date/time value, generate a datetime object from it and pass it along as the value for updating. The downside here is that I then need to know ahead of time which columns will have date values in them and I'd like to not have to manually track and code for such scenarios if possible. The other option (and what I ended up doing for now) is to modify the DateTimeValidator to automatically convert struct_time strings into datetime objects with the following bit of code: if isinstance(value, basestring): try: if len(value) == 0: return None else: try: return datetime.datetime.fromtimestamp(time.mktime(time.strptime(value))) except OverflowError: return datetime.datetime.fromtimestamp(0) except: raise validators.Invalid("expected a date/time string of type struct_time in the DateTimeCol '%s', got %s %r instead" % \ (self.name, type(value), value), value, state) This gets the job done for me and even generates a NULL if the string was zero length, and also handles situations where the Unix timestamp can't be properly generated due to an overflow error. Anyone have any thoughts? Is this something that would be appropriate for the main codebase or should I be shunting this into my own custom validator and keeping it external? Thanks in advance, Ray |
From: Oleg B. <ph...@ph...> - 2009-08-12 07:51:48
|
On Tue, Aug 11, 2009 at 04:53:12PM -0700, Ray Van Dolson wrote: > It seems like this worked in the past, but I could just be imagining > things :) Yes, long ago all column types accepted strings and passed them to the backend as is. Later it was decided to make conversion stricter. > I could write a simple function to check whether or not I'm on a column > in my CSV file that has a date/time value, generate a datetime object > from it and pass it along as the value for updating. I'd very much recommend that way. With this you can check that the values are really date/time values in your preferred format. > The other option (and what I ended up doing for now) is to modify the > DateTimeValidator You don't need to modify the builtin validator. You can create your own column type (inherited from DateTimeCol) with your own validator (inherited from DateTimeValidator). Or you can pass a validator to the column, and your validator will be stacked on top of the DateTimeValidator. See examples at http://svn.colorstudy.com/SQLObject/trunk/sqlobject/tests/test_validation.py Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |