#30355: Specifying custom manager doesn't work with prefetch
-------------------------------------+-------------------------------------
               Reporter:  Kyle       |          Owner:  nobody
  Mulka                              |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  1.11
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 When using prefetch and specifying a custom manager to use for a reverse
 relation, Django doesn't filter correctly. Here's an example:

 {{{
 class Business(models.Model):
     name = models.CharField(max_length=255)

     def approved_reviews(self):
         return self.review_set(manager='approved_reviews').all()


 class ApprovedReviewsManager(models.Manager):
     def get_queryset(self):
         return super().get_queryset().filter(status=Review.APPROVED)


 class Review(models.Model):
     NEW = 1
     APPROVED = 2
     STATUS_CHOICES = (
         (NEW, 'New'),
         (APPROVED, 'Approved'),
     )
     business = models.ForeignKey(Business)
     text = models.CharField(max_length=255)
     status = models.IntegerField(choices=STATUS_CHOICES, default=NEW)

     objects = models.Manager()
     approved_reviews = ApprovedReviewsManager()


 class ApprovedReviewsTest(TestCase):
     def test_with_prefetch(self):
         business = Business()
         business.save()

         review = Review()
         review.business = business
         review.save()

         businesses = Business.objects.prefetch_related('review_set').all()

         business = businesses[0]
         approved_reviews =
 business.review_set(manager='approved_reviews').all()

         self.assertEqual(len(approved_reviews), 0)
 }}}

 The full test project is available here:
 
https://github.com/mulka/django_prefetch_manager_bug/blob/master/review_site/tests.py

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30355>
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/048.9f3069634cfb2377c628d59ccd341596%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to