#22817: Missing custom methods on EmptyQuerySet
-------------------------------------+-------------------------------------
     Reporter:  benjaoming           |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.6
  (models, ORM)                      |               Resolution:
     Severity:  Release blocker      |             Triage Stage:  Accepted
     Keywords:                       |      Needs documentation:  0
    Has patch:  0                    |  Patch needs improvement:  0
  Needs tests:  0                    |                    UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------

Comment (by benjaoming):

 I'm going to mark this as invalid very shortly because I cannot recreate
 the bug.

 Django 1.7 style Manager...

 {{{
 from django.db import models
 from django.db.models.query import QuerySet


 class MyQuerySet(QuerySet):
     def custom_method(self):
         return self.filter(my_field=True)


 class MyModel(models.Model):
     objects = MyQuerySet.as_manager()
     my_field = models.BooleanField(default=False)

 }}}

 Output:

 {{{
 >>> models.MyModel.objects.custom_method()
 []

 >>> models.MyModel.objects.none().custom_method()
 []
 }}}

 This part works.

 Also works with Django <=1.6 style managers:


 {{{
 from django.db import models
 from django.db.models.query import QuerySet


 class MyManager(models.Manager):
     def get_empty_query_set(self):
         return MyEmptyQuerySet(model=self.model)

     def get_query_set(self):
         return MyQuerySet(self.model, using=self._db)

     def custom_method(self):
         return self.filter(my_field=True)


 class MyQuerySet(QuerySet):
     def custom_method(self):
         return self.filter(my_field=True)


 class MyEmptyQuerySet(QuerySet):
     def custom_method(self):
         return self


 class MyModel(models.Model):
     objects = MyManager()
     my_field = models.BooleanField(default=False)

 }}}


 Running on Django 1.7, the above code works:

 {{{
 >> from testapp import models
 >>> models.MyModel.objects.none().custom_method()
 []
 >>> models.MyModel.objects.custom_method()
 []
 >>> models.MyModel.objects.all().none().custom_method()
 []
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/22817#comment:7>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.ee63c69a620a771266c9c2f4d5128311%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to