RFC: Query Methods

2012-01-02 Thread Zachary Voase
At the moment it's very easy to add methods to individual models, just by putting a method on the model class itself which accepts 'self'. However, defining business logic on collections of records is currently pretty complicated — you have to create at least a custom Manager subclass, and if you w

Re: RFC: Query Methods

2012-01-02 Thread Russell Keith-Magee
On Tue, Jan 3, 2012 at 1:55 PM, Zachary Voase wrote: > At the moment it's very easy to add methods to individual models, just > by putting a method on the model class itself which accepts 'self'. > However, defining business logic on collections of records is > currently pretty complicated — you h

Re: RFC: Query Methods

2012-01-03 Thread Zachary Voase
On Jan 3, 7:01 am, Russell Keith-Magee wrote: > On Tue, Jan 3, 2012 at 1:55 PM, Zachary Voase wrote: > > At the moment it's very easy to add methods to individual models, just > > by putting a method on the model class itself which accepts 'self'. > > However, defining business logic on collectio

Re: RFC: Query Methods

2012-01-03 Thread Donald Stufft
I replied to the ticket, but I'll mention it here as well. django-model-utils has an implementation of something that achieves the same result. It was originally from http://paulm.us/post/3717466639/passthroughmanager-for-django and has since been added to https://github.com/carljm/django-model

Re: RFC: Query Methods

2012-01-03 Thread Carl Meyer
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi Zachary, On 01/02/2012 10:55 PM, Zachary Voase wrote: > At the moment it's very easy to add methods to individual models, just > by putting a method on the model class itself which accepts 'self'. > However, defining business logic on collections o

Re: RFC: Query Methods

2012-01-03 Thread Alex Gaynor
On Tue, Jan 3, 2012 at 11:43 AM, Carl Meyer wrote: > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > Hi Zachary, > > On 01/02/2012 10:55 PM, Zachary Voase wrote: > > At the moment it's very easy to add methods to individual models, just > > by putting a method on the model class itself which

Re: RFC: Query Methods

2012-01-03 Thread Carl Meyer
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 01/03/2012 10:42 AM, Alex Gaynor wrote: > I haven't analyzed your suggestion in detail Carl, but there is a good > reason for managers to exist: > querysets cache internally on iteration, managers are not directly > evaluable, but were they you cou

Re: RFC: Query Methods

2012-01-03 Thread Tai Lee
I have had difficulty in finding an easy way to add common methods to querysets and managers for multiple models in Django. I solved the problem in my projects by defining a custom `Manager` class which defines `use_for_related_fields = True` and also overrides `__getattr__()` to look for queryset

Re: RFC: Query Methods

2012-01-04 Thread Aaron Merriam
Tai, we use that same structure at my work and so far it has worked well. https://github.com/fusionbox/django-fusionbox/blob/master/fusionbox/db/models.py It also extends well. You can define queryset methods elsewhere and then inherit from them on multiple models to reuse those methods easily.

Re: RFC: Query Methods

2012-01-05 Thread Daniel Sokolowski
Forgive me if I am missing something --- I use custom managers extensively and do not see what your code can do that the django custom managers can't ? Can you provide an example please ? On Tue, Jan 3, 2012 at 12:55 AM, Zachary Voase wrote: > At the moment it's very easy to add methods to indi

Re: RFC: Query Methods

2012-01-05 Thread Russell Keith-Magee
On Fri, Jan 6, 2012 at 12:02 AM, Daniel Sokolowski wrote: > Forgive me if I am missing something --- I use custom managers extensively > and do not see what your code can do that the django custom managers can't ? > Can you provide an example please ? Zachary provided a complete example in his or

Re: RFC: Query Methods

2012-01-09 Thread Anssi Kääriäinen
On Jan 3, 7:55 am, Zachary Voase wrote: > At the moment it's very easy to add methods to individual models, just > by putting a method on the model class itself which accepts 'self'. > However, defining business logic on collections of records is > currently pretty complicated — you have to create

Re: RFC: Query Methods

2012-01-10 Thread Anssi Kääriäinen
On Jan 9, 10:51 pm, Anssi Kääriäinen wrote: > I have a very quickly crafted draft of the main idea > athttps://github.com/akaariai/django/compare/dynamic_man > > As you can see, the implementation is very simple. It is missing > manager autodiscovery, and the @querymethod decorator. > > I have

Re: RFC: Query Methods

2012-01-10 Thread Zachary Voase
It should be relatively simple to allow QuerySets to know which manager they came from, and then override __getattr__ to use the declared @querymethod from that manager (instead of the other way around). This avoids the unpickleable QuerySet subclass problem. On Jan 10, 8:54 am, Anssi Kääriäinen

Re: RFC: Query Methods

2012-01-10 Thread Anssi Kääriäinen
On Jan 10, 12:19 pm, Zachary Voase wrote: > It should be relatively simple to allow QuerySets to know which > manager they came from, and then override __getattr__ to use the > declared @querymethod from that manager (instead of the other way > around). This avoids the unpickleable QuerySet subc

Re: RFC: Query Methods

2012-01-10 Thread Carl Meyer
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi Anssi, On 01/10/2012 04:14 AM, Anssi Kääriäinen wrote: > However, I would like it if you could mix methods from different > managers. Maybe add qs.set_manager(manager_name), and so you could do: > SomeModel.objects.order_by('id').some_manager_metho

Re: RFC: Query Methods

2012-01-10 Thread Anssi Kääriäinen
On Jan 10, 7:10 pm, Carl Meyer wrote: > Having access to any manager method from any queryset, regardless of > which manager that queryset came from, seems to me like a highly > surprising anti-feature, not a benefit. Can you outline a realistic use > case for it? Basically any time when you have

Re: RFC: Query Methods

2012-01-12 Thread Zachary Voase
I strongly feel that switching around managers like that would be an example of overengineering. The user story which drove this RFC was based on an issue which has been encountered by lots of developers, but typically solved in a single way. Django's conservative style of development is very acco

Re: RFC: Query Methods

2012-01-12 Thread Etienne Robillard
On 01/12/2012 07:24 AM, Zachary Voase wrote: I strongly feel that switching around managers like that would be an example of overengineering. The user story which drove this RFC was based on an issue which has been encountered by lots of developers, but typically solved in a single way. Django's

Re: RFC: Query Methods

2012-01-12 Thread Anssi Kääriäinen
On Jan 12, 2:24 pm, Zachary Voase wrote: > I strongly feel that switching around managers like that would be an > example of overengineering. I tend to over-engineer things. It is easy to add the manager switching later on if needed. And it is easy to switch managers by just changing qs.manager i

Re: RFC: Query Methods

2012-05-10 Thread Michael Mior
Got interested in this after a referral from the tricket tracker. Couldn't this specific case be solved with a simple change to __getattr__ on QuerySet? def __getattr__(self, attr): try: qmethod = getattr(self.manager, attr).im_func is_querymethod = qmethod.is_querymethod