Skip to main content

Caches the groups a user is in so requests don't have to make calls to the database to check group status.

Project description

Caches the groups a user is in so requests don’t have to make calls to the database to check group status.


Currently supporting Django 1.8, 1.11, and 2.0 with Python 2.7/Python 3 where support for versions aligns with Django’s support. See the tox.ini file for specific Python and Django version pairings.


The full documentation is at


Install django-groups-cache:

pip install django-groups-cache

Add it to your INSTALLED_APPS:


Add the middleware to your MIDDLEWARE_CLASSES:


Checking in a Django Template if the user is in a group name:

{% if "admins" in request.groups_cache %}
  <a href="/admin">Admin Panel</a>
{% endif %}

# or use templatetag, note that templatetag is slower

{% load has_group %}

{% if request.user|has_group:"admins" %}
  <a href="/admin">Admin Panel</a>
{% endif %}

Turn on caching:

    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '',

See for more details on setting up memcached.


Using django.core.cache.backends.locmem.LocMemCache is not safe for production unless you are only running a single process (and odds are you aren’t).

See for more details.


  • Adds groups_cache property to request object
  • Provides templatetag has_group
  • Invalidates cache on User or Group model changes and on m2m groups ManyToManyField changes
  • Fully tested with high coverage

Running Tests

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox


Tools used in rendering this package:


0.5.5 (2017-01-13)

  • Bug fix for templatetag has_group
  • Tests added for 100% coverage of templatetag folder

0.5.5 (2017-01-12)

  • Omit from coverage report (not used but needed for django package)
  • Omit from coverage report (default apps file)

0.5.4 (2017-01-12)

  • Removal of py32/django1.8 support from Travis CI build

0.5.3 (2017-01-12)

  • Fixing .travis.yml file and CI builds

0.5.2 (2017-01-12)

  • Typo in README.rst

0.5.1 (2017-01-12)

  • Add support
  • Documentation updates

0.5.0 (2017-01-12)

  • Add requirements.txt to tox.ini so tests can run
  • Fix broken compatibility with Django 1.8/1.9 due to backwards incompatible changes in 1.9/1.10 code

0.4.0 (2017-01-12)

  • Add test suite that generates 100% coverage
  • Fixed a bug found by test suite with cache not invalidating on the groups ManyToManyField changing

0.3.1 (2017-01-11)

  • Documentation updates

0.3.0 (2017-01-11)

  • First stable and working release on PyPI.

0.1.0 (2017-01-11)

  • First release on PyPI.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-groups-cache, version 1.0.2
Filename, size File type Python version Upload date Hashes
Filename, size django_groups_cache-1.0.2-py2.py3-none-any.whl (10.4 kB) File type Wheel Python version 3.6 Upload date Hashes View
Filename, size django-groups-cache-1.0.2.tar.gz (8.1 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page