Suor / django-cacheops

A slick ORM cache with automatic granular event-driven invalidation.
BSD 3-Clause "New" or "Revised" License
2.11k stars 227 forks source link

Automatic caching not working in admin #287

Open MarkBird opened 6 years ago

MarkBird commented 6 years ago

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.

Suor commented 6 years ago

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

MarkBird commented 6 years ago

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?

Suor commented 6 years ago

Oh, caching is disabled in admin explicitely.

Suor commented 6 years ago

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.

MarkBird commented 6 years ago

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

Lepird commented 6 years ago

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

Suor commented 6 years ago

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.

gkapatia commented 5 years ago

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

c0d5x commented 4 years ago

please!

lampwins commented 4 years ago

@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.

paulonteri commented 3 years ago

This is needed!

physicalattraction commented 1 year ago

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
Suor commented 1 year ago

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.

rooterkyberian commented 11 months ago

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.