skillfed

django-modelcluster

Django extension to allow working with 'clusters' of models as a single unit, independently of the database

django-modelcluster v6.5 1.0M downloads/30d#4,521 on PyPI521
Permissive license BSD-3-Clause Active released

What it is and what it does

django-modelcluster solves the problem of working with related Django model objects before they exist in the database. Normally, Django foreign keys require both the parent and child records to be saved first; this package introduces ParentalKey and ParentalManyToManyField to hold related objects in memory until the parent is explicitly saved. This is useful for rendering previews of unsaved data, building draft model hierarchies, handling multi-step workflows, or constructing temporary object graphs for serialization or external systems.

The package extends ClusterableModel as the parent and uses ParentalKey in place of ForeignKey for one-to-many relations, and ParentalManyToManyField for many-to-many relations. Related objects are accessible via a subset of Django's QuerySet API (count, all, add) while in memory, and the entire cluster is persisted to the database in a single save() call. It also provides introspection functions to discover child relations on a model.

Use it for:

  • Render a preview of form data before the user confirms and saves the full model cluster to the database.
  • Build and manipulate a tree of draft models in a multi-step workflow without intermediate database writes.
  • Construct a temporary cluster of related objects, serialize it to JSON or another format, and send it to an external system.
  • Create a deep copy of a model and all its child relations using get_all_child_relations introspection.
  • Handle versioning or revision workflows where models exist in an incomplete state for extended periods.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

django-modelcluster extends Django models to work with clusters of related objects in memory before saving to the database, using ParentalKey and ParentalManyToManyField relations.

Yes. django-modelcluster is actively maintained, has no known vulnerabilities, carries a permissive license, and solves a real Django limitation with low install friction. It is well-suited for any Django project that needs to work with unsaved model clusters or draft states. The only caveat is verifying that your specific ORM patterns (beyond basic QuerySet operations) are supported.

Install

django-modelcluster on PyPI

pip

pip install django-modelcluster

uv

uv add django-modelcluster

poetry

poetry add django-modelcluster

Installing django-modelcluster

Before you install

Low friction: pure Python wheel with only django as a runtime dependency. Actively maintained with recent releases; repo shows 521 stars and current Python version support (3.10–3.14).

License in practice

BSD-3-Clause (permissive): you may use, modify, and distribute this package freely in commercial and private projects, provided you include the license text and do not hold the authors liable.

Quickstart

pip install django-modelcluster

from modelcluster.models import ClusterableModel
from modelcluster.fields import ParentalKey

class Band(ClusterableModel):
    name = models.CharField(max_length=255)

class BandMember(models.Model):
    band = ParentalKey('Band', related_name='members', on_delete=models.CASCADE)
    name = models.CharField(max_length=255)

beatles = Band(name='The Beatles')
beatles.members = [BandMember(name='John Lennon')]
print(beatles.members.count())  # 1, before save
beatles.save()  # now written to database

Requires Django as a runtime dependency and Python >= 3.10.

Verify before relying

  • Performance characteristics when working with large clusters of related objects in memory.
  • Compatibility with Django ORM features beyond the documented QuerySet subset (e.g., aggregations, annotations).
  • Whether ParentalManyToManyField supports batch operations or only single-object assignment.

Package facts

License BSD-3-Clause (permissive)
Python support supports the current Python release (>=3.10)
Install friction low — pure-Python wheel
Runtime dependencies 1 — django
Maintenance actively maintained — 105 days since the last release
Last repo commit
First released
Downloads 1,007,619/month — #4,521 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: django_modelcluster-6.5-py3-none-any.whl

Keywords: cluster, django, model

Development Status :: 5 - Production/StableEnvironment :: Web EnvironmentFramework :: DjangoIntended Audience :: DevelopersOperating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14

Tags

django in-memory model relationsdjango unsaved foreign key relationsdjango model clusteringdjango draft model statedjango parentalkey alternative to foreignkeydjango related objects without databasedjango model preview before save
django-extensionin-memory-relationsdraft-state

More Application Frameworks packages

fastapi

FastAPI is a Python web framework for building…

permissive · top 100 on PyPI

annotated-doc

Provides a way to document function parameters,…

permissive · top 100 on PyPI

textual

Textual is a Python framework for building…

permissive · top 100 on PyPI

typer

Typer builds command-line applications from…

permissive · top 1,000 on PyPI

mcp

Build and connect to Model Context Protocol…

permissive · top 1,000 on PyPI

Werkzeug

Werkzeug is a WSGI utility library providing…

permissive · top 1,000 on PyPI

django-solo

Django Solo provides a base model class and…

unclear · top 15,000 on PyPI

drf-writable-nested

Extends Django REST Framework serializers to…

permissive · top 15,000 on PyPI

django-ordered-model

Adds ordering and reordering capabilities to…

permissive · top 15,000 on PyPI

rest-framework-generic-relations

Provides Django REST Framework serializers that…

permissive · top 15,000 on PyPI

django-reverse-admin

Adds reverse inline support to Django admin for…

permissive · top 15,000 on PyPI

django-tree-queries

Query hierarchical tree structures in Django…

permissive · top 15,000 on PyPI

django-sortedm2m

Provides a drop-in replacement for Django's…

permissive · top 15,000 on PyPI

django-nonrelated-inlines

Provides Django admin inline classes that let…

permissive · top 15,000 on PyPI

django-soft-delete

Adds soft deletion to Django models by marking…

permissive · top 15,000 on PyPI

django-rest-polymorphic

Provides polymorphic serializers for Django…

permissive · top 5,000 on PyPI