django-cacheops
django-cacheops copied to clipboard
Automatic caching not working in admin
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.
DB backend doesn't matter. Maybe something else is misconfigured.
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?
Oh, caching is disabled in admin explicitely.
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.
Great, thanks for confirming - I've tested out non-admin queries and everything is working correctly and performance is great.
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
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.
hey! any update on enabling caching for admin through settings?
please!
@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.
This is needed!
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 errorAttributeError: '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
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.
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.