#17271: QuerySet.none() doesn't work well with subclasses of QuerySet
----------------------------------------------+--------------------
     Reporter:  andreypopp                    |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  SVN
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  1
Easy pickings:  1                             |      UI/UX:  0
----------------------------------------------+--------------------
 I use `QuerySet` subclassing to allow building complex composable queries
 out of smaller ones:
 {{{
 class MyQuerySet(QuerySet):
   def allowed(self, user):
     return self.filter(permissions__in=user.permissions)

   def have_feature(feature_name):
     return self.filter(feature__name=feature_name)
 ...

 objs = Obj.objects.allowed(user).have_feature("somefeature")
 }}}

 Sometimes I want to return `EmptyQuerySet` by calling `none()` to provide
 "fast-path" for some conditions, but unfortunately `EmptyQuerySet` doesn't
 have my custom methods, so call chain breaks after returning it:

 {{{
 class MyQuerySet(QuerySet):
   def allowed(self, user):
     if user.is_anonymous:
       return self.none()
     return self.filter(permissions__in=user.permissions)

   def have_feature(feature_name):
     return self.filter(feature__name=feature_name)
 ...

 objs = Obj.objects.allowed(user).have_feature("some feature")
 # AttributeError: EmptyQuerySet doesn't have have_feature attribute ...
 }}}

 I've attached a patch which instruments `EmptyQuerySet` subclass for each
 particular `QuerySet` implementation and returns its instance as a result
 of `none()` method call. I've also refactored code in `Manager.none()` to
 reuse existing code in `QuerySet.none()` and to parametrize `Manager()`
 constructor over `QuerySet` implementation to use.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/17271>
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 [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-updates?hl=en.

Reply via email to