this example fails because the object returned is not a Person instance,
it is the raw value of the column returned by your hybrid which in this
case is anonymously named, as the SQL expression is "x + y". You need
to give it the label of your choice for it to be present in the row in a
particular way. There is some logic these days inside of expression()
that seems to get in the way, so just subclass or use a plain descriptor:
class prefixed_property(hybrid_property):
def __init__(self, other_column, prefix, labelname):
self.other_column = other_column
self.prefix = prefix
self.labelname = labelname
def __get__(self, instance, owner):
if instance is None:
retval = self.prefix + getattr(owner, self.other_column.key)
return retval.label(self.labelname)
else:
return self.prefix + getattr(instance, self.other_column.key)
class Person(Base):
__tablename__ = "person"
id = Column(Integer, primary_key=True)
name = Column(String)
greeting = prefixed_property(name, "Hello ", "greeting")
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
>
http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See
http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to
sqlalchemy+...@googlegroups.com
> <mailto:
sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to
sqlal...@googlegroups.com
> <mailto:
sqlal...@googlegroups.com>.
> Visit this group at
https://groups.google.com/group/sqlalchemy.
> For more options, visit
https://groups.google.com/d/optout.