Thanks,
I did it using synonym
class TestModel(Base)
__tablename__='testtable'
_field1=Column('field1',Unicode(255))
def _set_field1(self, field1):
self._field1 = self.field1.upper()
def _get_field1(self):
return self._fiedl1
field1 = synonym('_field1', descriptor=property
(_get_field1,_set_field1))
thanks
drakkan
On 15 Giu, 17:52, Laurent Rahuel <[email protected]> wrote:
> Hi,
>
> You should take a look at MapperExtension. Then you'll be able to add
> method which would be called depending on the orm actions.
> For example, you'll be able to add your own custom before_insert or
> after_insert or after_delete methods.
>
> Regards,
>
> Laurent
>
> Le 15/06/2009 17:36, Didip Kerabat a écrit :
>
> > Quick note about __init__ method. SA select(..) or query(...) does not
> > call __init__(), so your to upper logic won't be executed then. If you
> > want it to be called every object construction you need to do this:
>
> > from sqlalchemy import orm
>
> > @orm.reconstructor
> > def some_function():
> > self.field1=field1.upper()
>
> > # Call that method inside __init__ as well
> > def __init__(self):
> > some_function()
>
> > Sorry for not answering the problem.
>
> > - Didip -
>
> > On Mon, Jun 15, 2009 at 1:43 AM, drakkan <[email protected]
> > <mailto:[email protected]>> wrote:
>
> > Hi all,
>
> > I'm migrating from django orm to sqlalchemy, in django was very simple
> > to override save method to do own stuff before save the value to
> > database, for example:
>
> > class TestModel(models.Model):
> > field1=models.CharField(max_length=255)
>
> > def save():
> > self.field1=self.field1.upper()
> > super(TestModel,self).save()
>
> > so when I add or modify an object it is ever converted to uppercase.
> > In sa this simple model become:
>
> > class TestModel(Base)
> > __tablename__='testtable'
> > field1=Column(Unicode(255))
>
> > def __init__(field1):
> > self.field1=field1.upper()
>
> > this way if I call the init method field1 is converted to upper but if
> > i modify the field I have to manually convert to upper. There is some
> > way to override save method as in django orm?
>
> > regards
> > drakkan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---