On Sun, Feb 26, 2006 at 12:38:38AM +0100, jt wrote:
> Suppose I have 3 columns in a table: a FloatCol, a CurrencyCol and a DateCol.
> 
> Storing something in the DateCol is easy: if it is a "date" (datetime
> object) or if it looks like a date (string containing an ISO-8601
> date) then everything works fine. Cool.

   Not exactly:

class Test(SQLObject):
   test = DateTimeCol()

Test.createTable()
t = Test(test="1967-12-21 17:44:01")

Traceback (most recent call last):
  File "./test1.py", line 15, in ?
    t = Test(test="1967-12-21 17:44:01")
  File 
"/usr/local/lib/python2.4/site-packages/SQLObject-0.7.1dev_r1534-py2.4.egg/sqlobject/main.py",
 line 1197, in __init__
    self._create(id, **kw)
  File 
"/usr/local/lib/python2.4/site-packages/SQLObject-0.7.1dev_r1534-py2.4.egg/sqlobject/main.py",
 line 1221, in _create
    self.set(**kw)
  File 
"/usr/local/lib/python2.4/site-packages/SQLObject-0.7.1dev_r1534-py2.4.egg/sqlobject/main.py",
 line 1080, in set
    kw[name] = dbValue = from_python(value, self._SO_validatorState)
  File 
"/usr/local/lib/python2.4/site-packages/SQLObject-0.7.1dev_r1534-py2.4.egg/sqlobject/col.py",
 line 928, in from_python
    (self.name, type(value), value), value, state)
formencode.api.Invalid: expected a datetime in the DateTimeCol 'test', got 
<type 'str'> '1967-12-21 17:44:01' instead

   DateColumns are special, though. You can do

class Test(SQLObject):
   test = DateCol()

Test.createTable()
t = Test(test="1967-12-21")

   but this works only because of some deep interactions between
DateValidator and DateTimeValidator. Do not rely on this. It could be
fixed in the future.

> Now if I try to store '12.345' in the FloatCol, it raises an exception
> stating that it wants a float object, not a string. This is annoying
> and counter intuitive. If it looks like a float, then it should be
> converted and stored quietly, as this is the case for date.

   No. If you decalred a float column - please assign floats to it. Int
for IntCol, datetime for DateTimeCol and so on.

> If I store anything in CurrencyCol then it always works fine. That is
> not so cool because I want the value to be checked against the
> appropriate format (I thought it would have a to_python method that
> gives me a decimal object, and a from_python that enforces that the
> to_python will work).

   Nobody was interested enough to write a vilidator. Want to be the
champion?

Oleg.
-- 
     Oleg Broytmann            http://phd.pp.ru/            [EMAIL PROTECTED]
           Programmers don't die, they just GOSUB without RETURN.


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
sqlobject-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to