On Oct 14, 2010, at 9:28 AM, Julien Cigar wrote: > Given all this "magic" with .with_polymorphic(), I'm now wondering if it's > really worth to have a new Query class (by subclassing orm.Query) per mapped > object just to apply some filters... > > At first I found quite elegant to have one Query object per mapped class, but > now I'm wondering if it's not better after all to have a bunch of > @staticmethod in the model ... > > What do you think ?
I generally think subclasses of Query should be used very conservatively, and that model specific behavior should be in the model, sure. > > On 10/13/2010 11:21, Julien Cigar wrote: >> On 10/12/2010 18:05, Julien Cigar wrote: >>> On 10/12/2010 17:09, Michael Bayer wrote: >>>> On Oct 12, 2010, at 7:39 AM, Julien Cigar wrote: >>>> >>>>> Hello, >>>>> >>>>> any idea why with >>>>> >>>>> # Query >>>>> >>>>> class BaseQuery(orm.Query): >>>>> @dynamic >>>>> def method_a(self): >>>>> ... >>>>> def method_b(self): >>>>> ... >>>>> >>>>> class FooQuery(BaseQuery): >>>>> ... >>>>> >>>>> class BarQuery(FooQuery): >>>>> @dynamic >>>>> def method_c(self): >>>>> ... >>>>> >>>>> # Models >>>>> >>>>> class BaseModel(object): >>>>> query = Session.query_property(BaseQuery) >>>>> >>>>> # >>>>> >>>>> myQuery = type('PolymorphicQuery, (content.BaseQuery, ), >>>>> func_list)(BaseModel) >>>>> >>>>> where "func_list" containing all the functions decorated by @dynamic >>>>> the following fail? : >>>>> >>>>> - myQuery.get(45) fails with: AttributeError: 'NoneType' object has >>>>> no attribute 'identity_map' >>>>> - myQuery.method_a().all() fails with: AttributeError: 'NoneType' >>>>> object has no attribute '_autoflush' >>>>> - etc >>>>> >> >> OK I think I found a solution, I need to pass session=Session.registry() >> to my custom query: >> >> >>> model.content.ContentQuery.__mro__ >> (<class 'amnesia.model.content.ContentQuery'>, <class >> 'amnesia.model.root.RootQuery'>, <class 'sqlalchemy.orm.query.Query'>, >> <type 'object'>) >> >>> PolymorphicQuery = type('PolymorphicQuery', >> (model.content.ContentQuery, ), {}) >> >>> q1 = PolymorphicQuery(model.Content) >> >>> q1.get(25) >> Traceback (most recent call last): >> File "<console>", line 1, in <module> >> File >> "/home/jcigar/venvs/pylons0.9.7/lib/python2.5/site-packages/SQLAlchemy-0.6.4-py2.5.egg/sqlalchemy/orm/query.py", >> line 595, in get >> return self._get(key, ident) >> File >> "/home/jcigar/venvs/pylons0.9.7/lib/python2.5/site-packages/SQLAlchemy-0.6.4-py2.5.egg/sqlalchemy/orm/query.py", >> line 1803, in _get >> instance = self.session.identity_map.get(key) >> AttributeError: 'NoneType' object has no attribute 'identity_map' >> >>> q2 = PolymorphicQuery(model.Content, session=meta.Session.registry()) >> >>> q2.get(25) >> <amnesia.model.event.Event object at 0x939046c> >> >> I hope I'm not doing something wrong :p >> >>>>> My goal is to be able to build a custom Query object to use with the >>>>> .with_polymorphic() function .. >>>> I've just grepped through all the source, examples and tests plus the >>>> wiki trying to find what @dynamic is. Seems like something I'd have >>>> come up with in the past but I've no clue at the moment what that is. >>>> >>>> >>> >>> Sorry, I forgot to mention that it's just a custom decorator of mine >>> which add the function name to a list ... forget about it, it's just to >>> build the third argument of type() (func_list in my case) >>> >> > > > -- > No trees were killed in the creation of this message. > However, many electrons were terribly inconvenienced. > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to sqlalch...@googlegroups.com. > To unsubscribe from this group, send email to > sqlalchemy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > > <jcigar.vcf> -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.