On 08/22/2016 03:20 AM, Jinghui Niu wrote:
I'm creating a mixin for my timestamp columns throughout my projects.
Internally, the mixin uses UTC strings to store timestamps, externally
it converts back and forth into local time using a second column that
stores timezone information. I'm studying the hybrid attribute section
in the documentation, but not sure if this use case would involve a
different expression between the instance level and class level.
As a general rule, what should I look into to determine whether I need
to use the hybrid_property.expression()?
expression is needed when you A. want to use your attribute in a Query
as part of filter() or similar and B. if the Python inside your hybrid
doesn't work as a SQLAlchemy expression.
Example 1: doesn't need expression - simple addition:
@hybrid_property
def a_plus_b(self):
return self.a + self.b
Example 2: does need expression - Python "if" statement needs to be CAST
on SQL
@hybrid_property
def conditional_a(self):
return "foo" if self.a == 'f' else "bar"
@conditional_a.expression
def conditional_a(cls):
return sql.case(("foo", cls.a == 'f'), else_="bar")
Do I need to first familiarize myself with the SQLalchemy's SQL
functions, such as func() etc., or there is some simpler rules? Thanks.
if you're working with date / timezone conversions in SQL then you
should figure out the SQL you want first, then at that point you
probably would need to use func. to get some SQL functions out of it,
it's pretty easy at that level.
--
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]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
--
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 post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.