Skip to main content

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

Release files for django-counter-field 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-counter-field 2.0.0
File Size Uploaded
django_counter_field-2.0.0.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-counter-field 2.0.0
File Interpreter ABI Platform
django_counter_field-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.4 kB

Release files / django_counter_field-2.0.0.tar.gz

Download URL django_counter_field-2.0.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b3cf9fd472512ac7365d4b202931abc2658b859c2767d847001e8235e1fda482
BLAKE2b-256 checksum
How to use checksums
9a45136c0d44b152c9e04a882fa514099311170541cb18401037719ce2e7fcb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 23, 2025.

Transparency log

Release files / django_counter_field-2.0.0-py3-none-any.whl

Download URL django_counter_field-2.0.0-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0d6ed40b319916459f3cfdfba843436e2319a063c97bb986c55525c0c63f8e8
BLAKE2b-256 checksum
How to use checksums
bd945abad2f21d8e6855c867b7b9c0c94b216c20b9164c0152fc8c0376f4f08d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 23, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

0.3.1

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page