Welcome to django-polymorphic’s documentation!¶
Django-polymorphic builds on top of the standard Django model inheritance. It makes using inherited models easier. 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"> ]
Features¶
Full admin integration.
ORM integration:
Support for ForeignKey, ManyToManyField, OneToOneField descriptors.
Support for proxy models.
Filtering/ordering of inherited models (
ArtProject___artist
).Filtering model types:
instance_of(...)
andnot_instance_of(...)
Combining querysets of different models (
qs3 = qs1 | qs2
)Support for custom user-defined managers.
Formset support.
Uses the minimum amount of queries needed to fetch the inherited models.
Disabling polymorphic behavior when needed.
Getting started¶
Advanced topics¶
- Formsets
- Migrating existing models to polymorphic
- Custom Managers, Querysets & Manager Inheritance
- Advanced features
- Filtering for classes (equivalent to python’s isinstance() ):
- Polymorphic filtering (for fields in inherited classes)
- Combining Querysets
- ManyToManyField, ForeignKey, OneToOneField
- Copying Polymorphic objects
- Using Third Party Models (without modifying them)
- Non-Polymorphic Queries
- About Queryset Methods
- Using enhanced Q-objects in any Places
- Nicely Displaying Polymorphic Querysets
- Restrictions & Caveats
- Changelog
- Changes in 3.1 (2021-11-18)
- Changes in 3.0.0 (2020-08-21)
- Changes in 2.1.2 (2019-07-15)
- Changes in 2.1.1 (2019-07-15)
- Changes in 2.1 (2019-07-15)
- Changes in 2.0.3 (2018-08-24)
- Changes in 2.0.2 (2018-02-05)
- Changes in 2.0.1 (2018-02-05)
- Changes in 2.0 (2018-01-22)
- Version 1.3.1 (2018-04-16)
- Version 1.3 (2017-08-01)
- Version 1.2 (2017-05-01)
- Version 1.1 (2017-02-03)
- Version 1.0.2 (2016-10-14)
- Version 1.0.1 (2016-09-11)
- Version 1.0 (2016-09-02)
- Version 0.9.2 (2016-05-04)
- Version 0.9.1 (2016-02-18)
- Version 0.9 (2016-02-17)
- Version 0.8.1 (2015-12-29)
- Version 0.8 (2015-12-28)
- Version 0.7.2 (2015-10-01)
- Version 0.7.1 (2015-04-30)
- Version 0.7 (2015-04-08)
- Version 0.6.1 (2014-12-30)
- Version 0.6 (2014-10-14)
- Version 0.5.6 (2014-07-21)
- Version 0.5.5 (2014-04-29)
- Version 0.5.4 (2014-04-09)
- Version 0.5.3 (2013-09-17)
- Version 0.5.2 (2013-09-05)
- Version 0.5.1 (2013-07-05)
- Version 0.5 (2013-04-20)
- Version 0.4.2 (2013-04-10)
- Version 0.4.1 (2013-04-10)
- Version 0.4 (2013-03-25)
- Version 0.3.1 (2013-02-28)
- Version 0.3 (2013-02-28)
- Version 0.2 (2011-04-27)
- Contributing
- API Documentation