#20625: Custom Chainable QuerySets
-------------------------------------+-------------------------------------
     Reporter:  danols               |                    Owner:  loic84
         Type:  New feature          |                   Status:  assigned
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |             Triage Stage:  Accepted
     Keywords:  QuerySet,            |      Needs documentation:  1
  models.Manager, chainable          |  Patch needs improvement:  1
    Has patch:  1                    |                    UI/UX:  0
  Needs tests:  1                    |
Easy pickings:  0                    |
-------------------------------------+-------------------------------------

Comment (by akaariai):

 Sorry for the Django super() requirement confusion.

 While writing my previous post I got an idea. I did some quick testing and
 it seems the idea needs to be presented. There are two key things in the
 idea:
   - Managers are created by CustomQuerySet.manager_cls() method - this is
 a factory constructing a new manager class.
   - The new manager class has dynamically created proxy methods for those
 methods in the CustomQuerySet that should be present in the manager.

 So, you could do something like this:
 {{{
 class MyQuerySet(models.QuerySet):
     def bar(self):
         return ...
     bar.manager = True

 class MyModel(Model):
     objects = MyQuerySet.as_manager()
 }}}

 Now, MyModel.objects is a custom Manager subclass which has a proxy method
 for bar(). And it really *has the method*, so super() works correctly.

 The API might not be optimal, but the idea that proxy methods are
 automatically created on dynamically created Manager classes should be
 considered carefully. In my opinion it offers better semantics than the
 `__getattr__` based approach.

 I wrote a very quick proof-of-concept patch. It is available from:
 https://github.com/akaariai/django/tree/manager_factory. The only
 interesting part of the patch is the method at
 
https://github.com/akaariai/django/commit/283af213d1873cefd642b6d9aeb97f285ef227c4#L2R44

-- 
Ticket URL: <https://code.djangoproject.com/ticket/20625#comment:20>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.5eada20e908b957945484457bc992c53%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to