Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using getters and setters with SQLAlchemy
class Table(Base):
id = Column(Integer, primary_key=True)
_name = Column('name', String(24))
@property
def name(self):
return self._name;
@name.setter
def name(self, value):
self._name = value.title()
#A query you might perform on the table.
#It won't work anymore :'(
query = db_session.query(Table)\
.filter(Table.name == 'Theron')
#You could do this, but it doesn't look as nice, is inconsistent with the "id"
#column, and you'd have to update all your existing code.
query = db_session.query(Table)\
.filter(Table._name == 'Theron')
from sqlalchemy.orm import synonym
class Table(Base):
id = Column(Integer, primary_key=True)
_name = Column('name', String(24))
@property
def name(self):
return self._name;
@name.setter
def name(self, value):
self._name = value.title()
name = synonym('_name', descriptor=name)
#A query you might perform on the table.
#Now it works again! :)
query = db_session.query(Table)\
.filter(Table.name == 'Theron')
class Table(Base):
id = Column(Integer, primary_key=True)
name = Column(String(24))
#A query you might perform on the table.
query = db_session.query(Table)\
.filter(Table.name == 'Theron')
@jmaicher

This comment has been minimized.

Copy link

jmaicher commented Aug 20, 2013

Thanks for the examples.
One remark: Instead of using @property you can use @hybrid_property and therewith avoid having to create the synonym.

from sqlalchemy.ext.hybrid import hybrid_property

class Table(Base):
    id = Column(Integer, primary_key=True)
    _name = Column('name', String(24))

    @hybrid_property
    def name(self):
        return self._name;

    @name.setter
    def name(self, value):
        self._name = value.title()
@baocaixiong

This comment has been minimized.

Copy link

baocaixiong commented Mar 12, 2015

@jmaicher
tks
👍

@lunemec

This comment has been minimized.

Copy link

lunemec commented Feb 23, 2016

@jmaicher yes, but hybrid_properties aren't supposed to have the same name as the column itself. The example you posted does not work. Please use the version with synonym it works flawlessly.

Thx for all the examples, I wish the official documentation would have these!

@arturoribes

This comment has been minimized.

Copy link

arturoribes commented Dec 13, 2017

Why not use event.listen instead of this? (I just used this guide to generate a slug upon setting an entity name).
Any ideas on the performance impact of using event?

@igorsobreira

This comment has been minimized.

Copy link

igorsobreira commented Feb 23, 2018

synonym also works with flask_admin form generation seamlessly. Thanks for posting this!

@josegonzalez

This comment has been minimized.

Copy link

josegonzalez commented Feb 1, 2019

Confirming that the synonym method works great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.