Skip to main content

Django counter field for tracking 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-field makes it easy to denormalize and keep such counters up to date.

Requirements

  • Python 3.14+

  • Django 5.0 - 6.x

Quick start

  1. Install django-counter-field:

    uv add django-counter-field
  2. Add “django_counter_field” to your INSTALLED_APPS setting:

    INSTALLED_APPS = [
        ...
        'django_counter_field',
    ]
  3. Add a CounterField to your model:

    from django_counter_field import CounterField
    
    
    class Article(models.Model):
        comment_count = CounterField()
  4. Add the CounterMixin to the related model:

    from django_counter_field import CounterMixin, connect_counter
    
    
    class Comment(CounterMixin, models.Model):
        article = models.ForeignKey(Article, on_delete=models.CASCADE)
  5. Connect the related foreign key field with the counter field:

    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 CounterField 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-field does that by default.

Backfilling

Often you will add a CounterField to a model that already has a large number of associated objects. When a counter is created, its value is initialized to zero. This value is likely incorrect. django-counter-field 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

uv add 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_field-2.0.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_counter_field-2.0.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file django_counter_field-2.0.0.tar.gz.

File metadata

  • Download URL: django_counter_field-2.0.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_counter_field-2.0.0.tar.gz
Algorithm Hash digest
SHA256 b3cf9fd472512ac7365d4b202931abc2658b859c2767d847001e8235e1fda482
MD5 d5cce98cbfa9e9a6a4f614f3a5c15290
BLAKE2b-256 9a45136c0d44b152c9e04a882fa514099311170541cb18401037719ce2e7fcb4

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_counter_field-2.0.0.tar.gz:

Publisher: publish.yaml on kajic/django-counter-field

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_counter_field-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_counter_field-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0d6ed40b319916459f3cfdfba843436e2319a063c97bb986c55525c0c63f8e8
MD5 bafca54eb7a69a4daf32e8f4770fb389
BLAKE2b-256 bd945abad2f21d8e6855c867b7b9c0c94b216c20b9164c0152fc8c0376f4f08d

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_counter_field-2.0.0-py3-none-any.whl:

Publisher: publish.yaml on kajic/django-counter-field

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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