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 want to chain those methods together you'll need a QuerySet subclass. An example of the desired behaviour, and the steps required for it, is shown in [1].
I originally created django-qmixin [2] to tackle this problem; you can define methods which will be present on the manager and all querysets produced therefrom, including on relations. However, the django-qmixin approach of creating a mixin and then including that on the Manager class object doesn't really gel with the rest of Django core. I've worked out two approaches which are easier for novices to understand, and match the idioms of the rest of Django. They both involve adding a @models.querymethod decorator, which would be applied to methods which operate on collections of records (i.e. querysets). It's an analog to Python's @classmethod. The first approach [3] involves adding these querymethods to the model class itself; the second [4] adds them to a manager subclass (but without the trouble of the QuerySet subclass). I prefer the former, for simplicity, but you may believe otherwise. I'm working on a proof-of-concept implementation, but I feel it's more important to agree on the interface and rationale beforehand. Any thoughts? [1]: https://gist.github.com/1553675#file_old_queryset_method.py [2]: https://github.com/zacharyvoase/django-qmixin [3]: https://gist.github.com/1553675#file_model_querymethod.py [4]: https://gist.github.com/1553675#file_manager_querymethod.py -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
