On 02/23/2016 01:41 AM, Edouard BERTHE wrote:
Hello everybody ! This is a question I have already posted on Stack Overflow <https://stackoverflow.com/questions/35546853/sqlalchemy-dynamic-customized-collection-class>, but as none is answering I decided to post it here too :) However I invite you to go to the Stack Overflow page, as it is maybe more clearly explained than here. I have a One-To-Many relation between an Article entity and a Post entity (which are the comments on the article). Each Post has a date attribute. What I would like to do is getting the posts related to an article between two dates directly from this article, using the following syntax : article.posts[start_date:end_date] With the "lazy" argument or "relationship", I can do : posts = relationship('Post', back_populates='article', lazy='dynamic') After that, the "posts" attribute isn't a list any longer, but a Query Object, which allows us to do: article.posts.filter(Post.date >= start_date, Post.date < end_date).all() Which is still not exactly what I'm looking for. I think I have to use the "collection_class" attribute of the relationship, by using a custom Class in which I would override the "__getitem__" method, but I don't know what to write in this function, because we don't have access to the query given by the `relationship` Object ! Does someone have an idea which could help me ?
if you're using "dynamic", the collection_class is not supported in that case. However, you can send query_class to relationship() when using dynamic loading. Build a subclass of Query, and override the __getitem__() method of it to look for dates within the slice object - when these are detected, convert it to the filter() version above and emit that.
With this new Query class, pass it along to your relationship() using the query_class parameter:
http://docs.sqlalchemy.org/en/rel_1_0/orm/relationship_api.html?highlight=query_class#sqlalchemy.orm.relationship.params.query_class
Thank you ! Edouard -- 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.
