python - SqlAlchemy UserDefinedType based on built-in type, with additional method -


i have class foo defined as:

class foo(object):     def bar(self):        pass 

i want extend sqlalchemy type:

class myunicode(foo, sqlalchemy.unicode):     def bar(self):         print "myunicode::bar" 

and define table this:

base = declarative_base() class table(base):    __tablename__ = "table"     first  = column(unicode(16))    second = column(myunicode(16)) 

finaly, want able use here:

t = query(table).first() t.bar() 

the problem typeengine, because debugger show t type of unicode not myunicode or sqlalchemy.unicode

i tried by:

class myunicode(foo, unicode):     def bar(self):         print "myunicode::bar"  class myunicode(sqlalchemy.unicode):      @property     def python_type(self):         return myunicode 

but doesn't work. suggestions?

i think you'll need overwrite def result_processor(self, dialect, coltype): because type database still unicode string , you'll have convert python type.

something this:

def result_processor(self, dialect, coltype):     def process(value):         if value not none:             return myunicode(value)   # value regular unicode string         else:             return myunicode()     return process 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -