Django QuerySet cache not set when using prefetch_related on reverse foreign key objects in query

811 Views Asked by At

Not sure if this is a bug in django or I am approaching this from the wrong angle.

I need to run a query that brings back all 'site' model objects but also some other model objects that relate via foreign key to the site model objects.

To do this I run:

sites = Site.objects.prefetch_related(
    'tanks', 'terminals', 'statuses',
).filter(company=company, **kwargs).order_by(*ordering)

This is great, it brings back all tanks, terminals and statuses objects that relate to each site in a single query. My problem is that if I then try to cache this queryset (thus allowing the user to download this queryset in XLS/CSV without needing to perform any other lookups), the cache is always set to None (i.e. it doesn't get set).

This is only happening when I pass model names that relate to the site (i.e. reverse FK). If I pass model names in that the site relates to or remove the prefetch_related call the cache does get set successfully. If I do this however the database gets hit at least 100 times when the queryset is evaluated.

So in a nutshell the issue appears to be that using prefetch_related on a queryset to pull in reverse FK lookups makes the caching of that queryset fall over. In addition this also fails when running _set on a queryset (e.g. company.site_set.filter(...))

I'm assuming it is something to do with no direct references in the site model for the objects that link to it but it would be useful to understand the underlying mechanics of it all.

Could anyone shed any light on this? I am using memcached in production.

0

There are 0 best solutions below