Thanks Mike. In future is it likely to have the instance level expression
and class level expression automatically translated by ORM? It will be so
much easier!

On Mon, Aug 22, 2016 at 7:20 AM, Mike Bayer <mike...@zzzcomputing.com>
wrote:

>
>
> 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 sqlalchemy+unsubscr...@googlegroups.com
>> <mailto:sqlalchemy+unsubscr...@googlegroups.com>.
>> To post to this group, send email to sqlalchemy@googlegroups.com
>> <mailto:sqlalchemy@googlegroups.com>.
>> 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 a topic in the
> Google Groups "sqlalchemy" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/to
> pic/sqlalchemy/AHNauPgot3c/unsubscribe.
> To unsubscribe from this group and all its topics, 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.
>

-- 
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