> <
https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/GlobalFilter>.
>
> seems it's exactly what I need!
>
> Two last questions:
> 1. Is it possible to specify "path" to get needed value from bound
> param, so I can bind request instance for each query in my framework and
> a model will get a value it needs. Something like:
> |
> favorite =relationship(
> User,
> primaryjoin=Book.id ==UserFavoriteBooks.book_id,
> secondaryjoin=and_(
> UserFavoriteBooks.user_id ==User.id,
> User.id ==bindparam("request",lambada req:
req.user.id)
> )
> )
> |
>
> query:
> |
> books =session.query(requested_class).params(request=request).all()
instead of params() I'd build a simple MapperOption that does what you
need. a bindparam() can use a lambda but it doesn't accept an argument
so that usage wouldn't work like that. Within your mapperoption you'd
extract the
req.user.id and add it to params(). You would need a
MapperOption in any case if you wanted lazy loading to work (the recipe
is a little intricate in this regard).
> |
>
> and wouldn't it break if some models doesn't specify any bindparam but I
> bound it to query?
you can have parameters in params() that aren't used, but also you can
look inside of query.column_descriptions to see if your entity is there.
> That's all because I build query automatically in my framework base on
> resource requested by user
>
> 2. Can I make a relationship that will result in bool value:
> favorite = relationship(User, ...., collection_class=bool)?
well a "bool" is not a "collection". You can have a list of scalar
values with the association proxy
(
https://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#simplifying-scalar-collections)
but that doesn't make much sense for a "bool" because there are only two
possible values.
"favorite" here looks like you have a Book and you want a list of all
the Users who have this book as a favorite? that would just be the list
of User objects then...
>
> Thanks
>
> On Monday, February 6, 2017 at 10:44:42 PM UTC+3, Mike Bayer wrote:
>
>
>
> On 02/06/2017 01:45 PM, Евгений Иванов wrote:
> > By fake I mean the field is not present in db, but present in
> model and
> > calculated when model is loaded. Favorite is a fields for Book
> model and
> > can be represented by something similar to:
> >
> > class Book(Base):
> > id = Column(...)
> > name = Column(...)
> > favorite_for = relationship(User, secondary=UserFavoriteBooks,
> > backref=favorite_books)
> > @property
> > def favorite(self)
> > return
request.user.id <
http://request.user.id> in [
u.id
> <
http://u.id> for u in self.favorite_for]
> >
> > request is global here.
> >
> > But I wan't it to be calculated once model is loaded, I think that
> could
> > be achieved by column_property by select() with case() to check if
> > current user is in the favorite_for list.
>
> why don't you use a relationship() with the bound parameter?
> there's a
> recipe for this at
>
https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/GlobalFilter
> <
https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/GlobalFilter>.
> > > <mailto:
sqlalchemy+...@googlegroups.com
> <javascript:> <javascript:>>.
> > <mailto:
sqlalchemy+...@googlegroups.com <javascript:>>.
> <mailto:
sqlalchemy+...@googlegroups.com>.