django-cacheops icon indicating copy to clipboard operation
django-cacheops copied to clipboard

Automatic caching not working in admin

Open MarkBird opened this issue 6 years ago • 14 comments

I'm using django-pyodbc-azure to connect to SQL Server. There isn't anything in the Cacheops docs that I could see that mentions anything about which backends are supported - but I've tried just adding

'*.*': {'ops': 'all', 'timeout': 60*60},

to the CACHEOPS config, which I think means everything should be automatically cached, and nothing gets cached at all, no errors, just nothing happens, all queries hit the database every time.

MarkBird avatar Jun 01 '18 11:06 MarkBird

DB backend doesn't matter. Maybe something else is misconfigured.

Suor avatar Jun 02 '18 03:06 Suor

Thanks for replying. I'm wondering if the problem is that Django Admin seems to ignore bespoke object managers? Can you confirm that cacheops supports caching on the Admin site?

This probably isn't the right place to ask these kind of questions - should I just try StackOverflow instead?

MarkBird avatar Jun 04 '18 14:06 MarkBird

Oh, caching is disabled in admin explicitely.

Suor avatar Jun 05 '18 07:06 Suor

This is historical decision, I wonder whether I should turn it off in next major version, but for now it stays this way. To cache in admin you need to overwrite .get_queryset() method and call .cache() on queryset yourself.

Suor avatar Jun 05 '18 07:06 Suor

Great, thanks for confirming - I've tested out non-admin queries and everything is working correctly and performance is great.

MarkBird avatar Jun 05 '18 08:06 MarkBird

Just adding a note on the caching of admin design choice, if you deem it good as an added feature maybe you can add it as an op per model ?

my logic here is that this would potentially be good for some models which are mainly config/setup related, but others like user model where admin is used to bulk view/alter users this would just flood redis with not needed data

Lepird avatar Jun 09 '18 00:06 Lepird

Thinking of more general approach to this. Using some form of aspects - mark them somehow and then configure to cache or not to cache things.

Suor avatar Jul 29 '18 09:07 Suor

hey! any update on enabling caching for admin through settings?

gkapatia avatar Mar 25 '19 10:03 gkapatia

please!

c0d5x avatar Aug 25 '20 08:08 c0d5x

@Suor I have run into this issue inadvertently by overriding get_queryset() but also using a m2m through model with an inline admin form.

Can you explain the reasoning as to why ModelAdmin querysets disable caching explicitly? I just want to better understand why this is today.

lampwins avatar Sep 26 '20 05:09 lampwins

This is needed!

paulonteri avatar Apr 21 '21 13:04 paulonteri

This is historical decision, I wonder whether I should turn it off in next major version, but for now it stays this way. To cache in admin you need to overwrite .get_queryset() method and call .cache() on queryset yourself. @Suor How would this work exactly? When I have this function, I get the error AttributeError: 'QuerySet' object has no attribute 'cache' in Django 3.2.16

    def get_queryset(self, request):
        queryset = super().get_queryset(request)
        queryset = queryset.cache()
        return queryset

physicalattraction avatar May 04 '23 09:05 physicalattraction

This might mean that cacheops was not installed properly, i.e. monkey patches were not applied, or this particular queryset does not descend from django.db.models.QuerySet, cacheops monkey patches that by adding .cache() to that among other things.

Suor avatar May 05 '23 09:05 Suor

I just wasted some time on this as well. Went into bug tracker thinking maybe there is a bug in a recent version or something. At very least it should be mention in README, but I would also vote for giving people an option to enable it for admin operation as well. Even better, make it enabled by default, as it is better for admin to be first person to see cache misbehaving than end-user.

rooterkyberian avatar Oct 31 '23 09:10 rooterkyberian