#30355: Specifying custom manager doesn't work with prefetch
-------------------------------------+-------------------------------------
Reporter: Kyle Mulka | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Daniel Hepper):
I've reproduced this behavior with the master branch, but I don't know if
this is actually a bug or just a case of a somewhat unintuitive API. This
should at least be documented in the section on using a custom reverse
manager: https://docs.djangoproject.com/en/2.2/topics/db/queries/#using-a
-custom-reverse-manager
A possible workaround would be to use a `django.db.models.Prefetch`
object:
{{{
class ApprovedReviewsTest(TestCase):
def test_with_prefetch(self):
business = Business()
business.save()
review = Review()
review.business = business
review.save()
prefetch_approved_reviews = Prefetch('review_set',
queryset=Review.approved_reviews.all())
businesses =
Business.objects.prefetch_related(prefetch_approved_reviews).all()
business = businesses[0]
approved_reviews =
business.review_set(manager='approved_reviews').all()
self.assertEqual(len(approved_reviews), 0)
}}}
In this example, you don't even have to specify the custom manager,
`business.review_set.all()` would give you the same result.
The underlying issue is that there is only a single prefetched object
cache per field, see
https://github.com/django/django/blob/de7f6b51b21747e19e90d9e3e04e0cdbf84e8a75/django/db/models/fields/related_descriptors.py#L607
--
Ticket URL: <https://code.djangoproject.com/ticket/30355#comment:1>
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/063.d8d479fa2cd6e56a53650db26820ae8f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.