On Friday, November 11, 2016 at 2:22:28 PM UTC, Simon King wrote:
>
> On Fri, Nov 11, 2016 at 1:53 PM, Michael Williamson <mic...@healx.io 
> <javascript:>> wrote: 
> > 
> >> I think your code is basically fine, you've just got a mistake on the 
> >> last line. Presumably you meant to query Person, not Person.born? 
> > 
> > 
> > I want Person.born so that I don't have to get the entire object. It 
> doesn't 
> > make much difference in this example, but is quite important for us in 
> > practice when we're grabbing multiple expressions. 
> > 
> >> 
> >> hybrid_property does't care about the name of the "fget" function, it 
> >> just calls it, passing the instance as the only parameter: 
> >> 
> >> 
> >> 
> https://bitbucket.org/zzzeek/sqlalchemy/src/f2eb4aac9517a3775411c2ecf0f588ffd0d790f6/lib/sqlalchemy/ext/hybrid.py?at=master&fileviewer=file-view-default#hybrid.py-744
>  
> >> 
> > 
> > The call to util.update_wrapper sets the property to have the same name 
> as 
> > the fget function, which in turn is used when creating the SQL 
> expression 
> > (the self.__name__ expression in the comparator method), which 
> determines 
> > the label of the value in the result object. 
> > 
>
> OK, I'm probably missing something. I don't have access to PG right 
> now, so I couldn't run your code directly. Having said that, I'm 
> surprised that the comparator object is even invoked in the 
> expression: 
>
>     session.query(Person.born).one().born == '1881' 
>
> ...because "session.query(Person.born).one()" returns a tuple-ish 
> object with 1 element, which won't have a "born" attribute. 
>

It's a namedtuple-ish object -- it behaves as a tuple, but you can also get 
values out by name. The comparator is invoked during construction of the 
hybrid property. Specifically, the constructor calls self.expression, which 
then calls self.comparator.
 

> Anyway, to answer your original question, would it be sufficient to 
> update the __name__ attribute of your instance_get function inside 
> json_property? 
>
> ie. 
>
> def json_property(json_column, name, type_=None): 
>     def instance_get(self): 
>         return getattr(self, json_column.key)[name] 
>     instance_get.__name__ = name 
>

Unfortunately not: the JSON property may not have the same name as the 
attribute e.g.

year_born = json_property(data, "yearBorn", Integer())
 

> Simon 
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to