Skip to main content

ddjango-counter-cache-fuxy makes it extremely easy to denormalize and keep track of related model counts.

Project description

https://travis-ci.org/enjoy2000/django-counter-cache-field.svg?branch=master https://coveralls.io/repos/github/enjoy2000/django-counter-cache-field/badge.svg

It is sometimes useful to cache the total number of objects associated with another object through a ForeignKey relation. For example the total number of comments associated with an article.

django_counter_cache_fuxy makes it easy to denormalize and keep such counters up to date.

Quick start

  1. Install django_counter_cache_fuxy:

    pip install django_counter_cache_fuxy
  2. Add “django_counter_cache_fuxy” to your INSTALLED_APPS setting:

    INSTALLED_APPS = (
        ...
        'django_counter_cache_fuxy',
    )
  3. Add a CounterCacheField to your model:

    from django_counter_cache_fuxy import CounterCacheField
    
    
    class Article(models.Model):
        comment_count = CounterCacheField()
  4. Connect the related foreign key field with the counter field in your signals:

    connect_counter('comment_count', Comment.article)

Whenever a comment is created the comment_count on the associated Article will be incremented. If the comment is deleted, the comment_count will be automatically decremented.

Overview

Creating a new counter requires three simple steps:

  1. Add a CounterCacheField field to the parent model.

  2. Add the CounterMixin mixin to the child model.

  3. Use connect_counter to connect the child model with the new counter.

Most counters are simple in the sense that you want to count all child objects. Sometimes, however, objects should be counted based on one or several conditions. For example you may not wish to count all comments on an article but only comments that have been approved. You can create conditional counters by providing a third is_in_counter_func argument to connect_counter:

connect_counter(‘comment_count’, Comment.article, lambda comment: comment.is_approved)

The is_in_counter_func function will be called with Comment objects and must return True if the given comment should be counted. It must not concern itself with checking if the comment is deleted or not, django_counter_cache_fuxy does that by default.

Backfilling

Often you will add a CounterCacheField to a model that already has a large number of associated objects. When a counter is created, it’s value is initialized to zero. This value is likely incorrect. django_counter_cache_fuxy provides a couple of management commands that allow you to rebuild the value of a counter:

  1. List all available counters:

    $ python manage.py list_counters

  2. Rebuild a counter using one of the counter names given by list_counters:

    $ python manage.py rebuild_counter <counter_name>

Note: The rebuild_counter management command will only update counters on objects that have at least one child object. For example articles with at least one comment. Articles with no comments will not be updated. This is a conscious limitation; the use cases for such a feature seem very limited, if existent at all.

Documentation

$ pip install Sphinx $ cd docs $ make html Open build/html/index.html

Project details


Download files

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

Source Distribution

django-counter-cache-fuxy-0.1.3.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

django_counter_cache_fuxy-0.1.3-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file django-counter-cache-fuxy-0.1.3.tar.gz.

File metadata

File hashes

Hashes for django-counter-cache-fuxy-0.1.3.tar.gz
Algorithm Hash digest
SHA256 fbbce8e64295de875de2dba3eb67d9d19a82509dbd9e8cd7b670e75cfc20ff4f
MD5 d9dd0580e8dd5ec721df84f5f491457f
BLAKE2b-256 f4ba2b679aacdf28f1e027bbf23f6491758ab81daa46a8d7179731f2fbcdfcae

See more details on using hashes here.

File details

Details for the file django_counter_cache_fuxy-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_counter_cache_fuxy-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 153ec6125ff2443fa508bf1f9477affb61ece610cdf967226e7befd5412a7c4f
MD5 40db83d1c3c4bb512782c27cd6fcc8ae
BLAKE2b-256 46352c5a81765d6137ef2fb4e7a0bca081944552080872bd46141a4825a72e24

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page