MCPcopy
hub / github.com/django-commons/django-polymorphic

github.com/django-commons/django-polymorphic @v4.11.5 sqlite

repository ↗ · DeepWiki ↗ · release v4.11.5 ↗
1,636 symbols 6,229 edges 171 files 667 documented · 41%
README

django-polymorphic

License: BSD Ruff PyPI version PyPI pyversions PyPI djversions PyPI status PyPI - Types Documentation Status Code Cov Test Status Lint Status Published on Django Packages OpenSSF Scorecard


Postgres MySQL MariaDB SQLite Oracle


Polymorphic Models for Django

django-polymorphic simplifies using inherited models in Django projects. When a query is made at the base model, the inherited model classes are returned.

When we store models that inherit from a Project model...


    >>> Project.objects.create(topic="Department Party")
    >>> ArtProject.objects.create(topic="Painting with Tim", artist="T. Turner")
    >>> ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")

...and want to retrieve all our projects, the subclassed models are returned!


    >>> Project.objects.all()
    [ <Project:         id 1, topic "Department Party">,
      <ArtProject:      id 2, topic "Painting with Tim", artist "T. Turner">,
      <ResearchProject: id 3, topic "Swallow Aerodynamics", supervisor "Dr. Winter"> ]

Using vanilla Django, we get the base class objects, which is rarely what we wanted:


    >>> Project.objects.all()
    [ <Project: id 1, topic "Department Party">,
      <Project: id 2, topic "Painting with Tim">,
      <Project: id 3, topic "Swallow Aerodynamics"> ]

This also works when the polymorphic model is accessed via ForeignKeys, ManyToManyFields or OneToOneFields.

Features

  • Full admin integration.
  • ORM integration:

  • support for ForeignKey, ManyToManyField, OneToOneField descriptors.

  • Filtering/ordering of inherited models (ArtProject___artist).
  • Filtering model types: instance_of(...) and not_instance_of(...)
  • Combining querysets of different models (qs3 = qs1 | qs2)
  • Support for custom user-defined managers.
  • Uses the minimum amount of queries needed to fetch the inherited models.
  • Disabling polymorphic behavior when needed.

Note: While django-polymorphic makes subclassed models easy to use in Django, we still encourage to use them with caution. Each subclassed model will require Django to perform an INNER JOIN to fetch the model fields from the database. While taking this in mind, there are valid reasons for using subclassed models. That's what this library is designed for!

For more information, see the documentation at Read the Docs.

Installation

    $ pip install django-polymorphic
INSTALLED_APPS = [
  ...
  "django.contrib.contenttypes",  # we rely on the contenttypes framework
  "polymorphic"
]

License

django-polymorphic uses the same license as Django (BSD-like).

Core symbols most depended-on inside this repo

all
called by 284
src/polymorphic/managers.py
filter
called by 155
src/polymorphic/managers.py
delete
called by 97
src/polymorphic/models.py
order_by
called by 66
src/polymorphic/query.py
non_polymorphic
called by 61
src/polymorphic/query.py
save
called by 59
src/polymorphic/models.py
polymorphic_modelformset_factory
called by 38
src/polymorphic/formsets/models.py
concrete_descendants
called by 36
src/polymorphic/utils.py

Shape

Method 826
Class 718
Function 91
Route 1

Languages

Python100%
TypeScript1%

Modules by API surface

src/polymorphic/tests/models.py257 symbols
src/polymorphic/tests/test_missing_coverage.py154 symbols
src/polymorphic/tests/test_orm.py115 symbols
src/polymorphic/tests/test_admin.py112 symbols
src/polymorphic/tests/deletion/models.py87 symbols
src/polymorphic/tests/examples/integrations/drf/test.py64 symbols
src/polymorphic/tests/test_formsets.py51 symbols
src/polymorphic/tests/test_migrations/test_on_delete.py40 symbols
src/polymorphic/tests/test_templatetags.py37 symbols
src/polymorphic/query.py33 symbols
src/polymorphic/tests/admintestcase.py24 symbols
src/polymorphic/tests/admin.py24 symbols

Dependencies from manifests, versioned

Django4.2 · 1×
django-extra-views0.14.0 · 1×
sphinx_rtd_theme2.0.0 · 1×
sphinxcontrib-django2.5 · 1×
typing_extensions4.12.0 · 1×

Datastores touched

(mysql)Database · 1 repos

For agents

$ claude mcp add django-polymorphic \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact