Return two SqlAlchemy Columns concatenated
Use the hybrid extension:
from sqlalchemy.ext.hybrid import hybrid_property
class Person(Base):
# ...
@hybrid_property
def name(self):
return '{0} {1}'.format(self.first_name, self.last_name)
@name.setter
def name(self, value):
self.first_name, self.last_name = value.split(' ', 1)
@name.expression
def name(cls):
return db.func.concat(cls.first_name, ' ', cls.last_name)
@davidism suggested using hybrid extension, whilst I say, why not use SQLAlchemy's column_property
function?
Example:
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
firstname = Column(String(50))
lastname = Column(String(50))
fullname = column_property(firstname + " " + lastname)