easy enough to create a function
def dict_getter(attr, key):
@hybrid_property
def get(self):
return getattr(self, attr).get(key)
@get.expression
def get(self):
return getattr(self, attr)[key]
return get
class User(Base):
phone_number_2= dict_getter("details", "phone_num_2")
probably a bunch of ways to organize that
On Mon, Sep 23, 2019, at 5:37 PM, Gary L wrote:
> I have a small model with a 'json' column called details and several hybrid
> properties to abstract getting from it safely:
>
> User(Base):
>
> @hybrid_property
> def phone_number_2(self):
> return self.details.get('phone_num_2')
>
> However when I use this in a query, in the class context, I get an error as
> sqlalchemy does not understand the .get method for python dictionaries.
> That's all fine as I can define hybrid expressions as such:
>
> @phone_numer_2.expression
> def phone_number_2(cls):
> return self.details['phone_num_2']
>
>
> This works fine but the only issue is I have a lot of similar columns I'd
> like to pull out and writing the expression every time seems cumbersome. Is
> there a convenient way to abstract this in Sqlalchemy? Some way I could tell
> sqlalchemy what .get means?
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sqlalchemy/da8d4fc0-7c99-470c-b710-f3a238fc823a%40googlegroups.com
>
> <https://groups.google.com/d/msgid/sqlalchemy/da8d4fc0-7c99-470c-b710-f3a238fc823a%40googlegroups.com?utm_medium=email&utm_source=footer>.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/12f4d88c-98ff-4b66-b31a-7cefb2b91896%40www.fastmail.com.