ddjango-counter-cache-fuxy makes it extremely easy to denormalize and keep track of related model counts.
Project description
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
Install django_counter_cache_fuxy:
pip install django_counter_cache_fuxy
Add “django_counter_cache_fuxy” to your INSTALLED_APPS setting:
INSTALLED_APPS = ( ... 'django_counter_cache_fuxy', )
Add a CounterCacheField to your model:
from django_counter_cache_fuxy import CounterCacheField class Article(models.Model): comment_count = CounterCacheField()
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:
Add a CounterCacheField field to the parent model.
Add the CounterMixin mixin to the child model.
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:
List all available counters:
$ python manage.py list_counters
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-counter-cache-fuxy-0.1.3.tar.gz
.
File metadata
- Download URL: django-counter-cache-fuxy-0.1.3.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbbce8e64295de875de2dba3eb67d9d19a82509dbd9e8cd7b670e75cfc20ff4f |
|
MD5 | d9dd0580e8dd5ec721df84f5f491457f |
|
BLAKE2b-256 | f4ba2b679aacdf28f1e027bbf23f6491758ab81daa46a8d7179731f2fbcdfcae |
File details
Details for the file django_counter_cache_fuxy-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: django_counter_cache_fuxy-0.1.3-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 153ec6125ff2443fa508bf1f9477affb61ece610cdf967226e7befd5412a7c4f |
|
MD5 | 40db83d1c3c4bb512782c27cd6fcc8ae |
|
BLAKE2b-256 | 46352c5a81765d6137ef2fb4e7a0bca081944552080872bd46141a4825a72e24 |