#17270: methods of the manager on subqueries QuerySet objects
-------------------------------------+-------------------------------------
     Reporter:  Wojciech Banaś       |      Owner:  fizista@…
  <fizista@…>                        |     Status:  new
         Type:  New feature          |    Version:
    Component:  Database layer       |   Keywords:  queryset manager
  (models, ORM)                      |  Extends
     Severity:  Normal               |  Has patch:  1
 Triage Stage:  Unreviewed           |      UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------
 Extends the operation methods of the manager on subqueries QuerySet
 objects.

 The "standard manager" you can only like this:

 {{{
 SomeModel.object.some_method_manager().all().filter(...). ...
 }}}


 But this does not work:

 {{{
 SomeModel.object.all().some_method_manager()
 }}}

 And this it works with the use of this class / patches:

 {{{
 
SomeModel.object.all().some_method_manager().filter(...).some_method_manager2().
 ...
 
SomeModel.object.some_method_manager().some_method_manager2().some_method_manager3().
 ...
 }}}

 Extension of the methods manager on all child objects "QuerySeth".

 Example:

 * class model
 {{{
     class Animals(models.Model):
         type = models.CharField('Type of animal') # dog, cat, elephant
         color = models.CharField('Colour Name')

         objects = AnimalsManager()
 }}}

 * class manager

 {{{
     class AnimalsManager(ManagerQuerySetDecorator):

         def get_dogs(self):
             return self.get_query_set().filter(type='dog')
         get_dogs.queryset_method = True

         def get_cats(self):
             return self.get_query_set().filter(type='cat')
         get_cats.queryset_method = True

         def get_white_animals(self):
             return self.get_query_set().filter(color='white')
         get_white_animals.queryset_method = True

         def get_black_animals(self):
             return self.get_query_set().filter(color='black')
         get_black_animals.queryset_method = True

         def get_brown_animals(self):
             return self.get_query_set().filter(color='black')

 }}}

 * add models data

 {{{
     Animals(type='dog', color='black').save()
     Animals(type='dog', color='bown').save()
     Animals(type='dog', color='white').save()
     Animals(type='cat', color='black').save()
     Animals(type='cat', color='bown').save()
     Animals(type='cat', color='white').save()

 }}}

 * examples of actions a new manager

 {{{
     animals = Animals.objects.get_black_animals().get_dogs()
     # return list black dogs

     animals = Animals.objects.get_white_animals().get_cats()
     # return list white cats

     # When ffff equals False, or not defined (as it is in
     # the original model manager), we can not perform such an operation:
     animals = Animals.objects.get_cats().get_brown_animals()
     # return:
     # AttributeError: 'QuerySet' object has no attribute
 'get_brown_animals'

     # but:
     animals = Animals.objects.get_brown_animals().get_cats()
     # return brown cats, because get_cat() present in all parent object
 instances.
 }}}

 In Annex file a class action extends the manager.

 If there is interest in this patch, then add in the future full of tests,
 and prepare a ready-made patch for django code.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/17270>
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 post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to