On Wed, Sep 16, 2009 at 05:32:06PM +0200, Frank Wagner wrote:
> database didn't have a charset declared at creation time
> (although it is de facto in iso-8859-1).
> so this was just the first working way that i could figure out.
> 
> i guess the UnicodeCol doesn't do it in a very different way?
> >
> > NAME = UnicodeCol(length=40, dbName="NAME", dbEncoding='iso-8859-1')

   It uses a different implementation internally based on
validators/converters, but UnicodeValidator does exactly that - converts
strings from DB to unicode, and converts unicode to strings for DB. See
col.py.

   BTW, speaking about validators...

> def _get_NAME(self):
>  return self._SO_get_NAME() and unicode(self._SO_get_NAME(),
> 'iso-8859-1').strip() or u''

   UnicodeCol only converts strings from/to unicode, so if you need
to strip and replace None's with u'' you have to do it yourself, but the
way to it, especially for many columns, is to create your own
validator/converter:

from col import validators

class StripValidator(validators.Validator):
    def to_python(self, value, state):
        if value is None:
            return None # or u'' if you really want it
        if isinstance(value, basestring):
            return value.strip()
        raise validators.Invalid("expected a str or unicode in column '%s', got 
%s %r instead" % \
                (self.name, type(value), value), value, state)


   And now you can use it in as many columns as you want:

NAME = UnicodeCol(length=40, dbName="NAME", dbEncoding='iso-8859-1',
                     validator=StripValidator(name="NAME"))
SURNAME = UnicodeCol(length=40, dbName="SURNAME", dbEncoding='iso-8859-1',
                     validator=StripValidator(name="SURNAME"))

   SQLObject stacks validators so your validator will be used along with
UnicodeValidator in UnicodeCol, not instead of it.

Oleg.
-- 
     Oleg Broytmann            http://phd.pp.ru/            p...@phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to