Suor / django-cacheops

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

Skip setup step if `_install_cacheops` not present #246

Closed srtonz closed 7 years ago

srtonz commented 7 years ago

I found that django-cacheops refuses to work with certain DB backends that aren't using Django's QuerySet or Manager classes.

The problem is that cacheops attempts setup for all models registered with Django (here: https://github.com/Suor/django-cacheops/blob/master/cacheops/query.py#L548) . If it can't install (because _install_cacheops isn't set), the whole processes crashes.

I wonder if you could wrap that part in a try..except like so:

for model in apps.get_models(include_auto_created=True):
    try:
        model._default_manager._install_cacheops(model)
    except AttributeError:
        logger.warning('Could not install cacheops for {}'.format(model))
        pass
Suor commented 7 years ago

Can you bring in an example? What kind of backend you use? How backend can change querysets? And what exception with stacktrace you get?

srtonz commented 7 years ago
File "/vagrant/env/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/vagrant/env/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/vagrant/env/lib/python3.5/site-packages/django/apps/registry.py", line 116, in populate
    app_config.ready()
  File "/vagrant/env/lib/python3.5/site-packages/cacheops/__init__.py", line 18, in ready
    install_cacheops()
  File "/vagrant/env/lib/python3.5/site-packages/funcy/flow.py", line 172, in wrapper
    return func(*args, **kwargs)
  File "/vagrant/env/lib/python3.5/site-packages/cacheops/query.py", line 569, in install_cacheops
    model._default_manager._install_cacheops(model)
AttributeError: 'DjangoCassandraQuerySet' object has no attribute '_install_cacheops'

There are multiple possibilities this can fail, I think it'd be nice if cacheops could handle it gracefully. In the case of django-cassandra-engine, there's no difference between the manager and the queryset class. Its QuerySet doesn't inherit from Django's Manager or QuerySet and is therefore not monkeypatched.

Suor commented 7 years ago

Should be fixed in 037e23a.