| 1 |
# django-polymorphic |
| 2 |
|
| 3 |
[](https://opensource.org/license/bsd-3-clause) |
| 4 |
[](https://github.com/astral-sh/ruff) |
| 5 |
[](https://pypi.python.org/pypi/django-polymorphic/) |
| 6 |
[](https://pypi.python.org/pypi/django-polymorphic/) |
| 7 |
[](https://pypi.org/project/django-polymorphic/) |
| 8 |
[](https://pypi.python.org/pypi/django-polymorphic) |
| 9 |
[](https://pypi.python.org/pypi/django-polymorphic) |
| 10 |
[](http://django-polymorphic.readthedocs.io/?badge=latest/) |
| 11 |
[](https://codecov.io/github/django-commons/django-polymorphic?branch=main) |
| 12 |
[](https://github.com/django-commons/django-polymorphic/actions/workflows/test.yml?query=branch:main) |
| 13 |
[](https://github.com/django-commons/django-polymorphic/actions/workflows/lint.yml?query=branch:main) |
| 14 |
[](https://djangopackages.org/packages/p/django-polymorphic/) |
| 15 |
[](https://securityscorecards.dev/viewer/?uri=github.com/django-commons/django-polymorphic) |
| 16 |
|
| 17 |
--------------------------------------------------------------------------------------------------- |
| 18 |
|
| 19 |
[](https://www.postgresql.org/) |
| 20 |
[](https://www.mysql.com/) |
| 21 |
[](https://mariadb.org/) |
| 22 |
[](https://www.sqlite.org/) |
| 23 |
[](https://www.oracle.com/database/) |
| 24 |
|
| 25 |
--------------------------------------------------------------------------------------------------- |
| 26 |
|
| 27 |
## Polymorphic Models for Django |
| 28 |
|
| 29 |
[django-polymorphic](https://pypi.python.org/pypi/django-polymorphic) simplifies using inherited models in [Django](https://djangoproject.com) projects. When a query is made at the base model, the inherited model classes are returned. |
| 30 |
|
| 31 |
When we store models that inherit from a ``Project`` model... |
| 32 |
|
| 33 |
```python |
| 34 |
|
| 35 |
>>> Project.objects.create(topic="Department Party") |
| 36 |
>>> ArtProject.objects.create(topic="Painting with Tim", artist="T. Turner") |
| 37 |
>>> ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter") |
| 38 |
``` |
| 39 |
|
| 40 |
...and want to retrieve all our projects, the subclassed models are returned! |
| 41 |
|
| 42 |
```python |
| 43 |
|
| 44 |
>>> Project.objects.all() |
| 45 |
[ <Project: id 1, topic "Department Party">, |
| 46 |
<ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">, |
| 47 |
<ResearchProject: id 3, topic "Swallow Aerodynamics", supervisor "Dr. Winter"> ] |
| 48 |
``` |
| 49 |
|
| 50 |
Using vanilla Django, we get the base class objects, which is rarely what we wanted: |
| 51 |
|
| 52 |
```python |
| 53 |
|
| 54 |
>>> Project.objects.all() |
| 55 |
[ <Project: id 1, topic "Department Party">, |
| 56 |
<Project: id 2, topic "Painting with Tim">, |
| 57 |
<Project: id 3, topic "Swallow Aerodynamics"> ] |
| 58 |
``` |
| 59 |
|
| 60 |
This also works when the polymorphic model is accessed via |
| 61 |
ForeignKeys, ManyToManyFields or OneToOneFields. |
| 62 |
|
| 63 |
### Features |
| 64 |
|
| 65 |
* Full admin integration. |
| 66 |
* ORM integration: |
| 67 |
|
| 68 |
* support for ForeignKey, ManyToManyField, OneToOneField descriptors. |
| 69 |
* Filtering/ordering of inherited models (``ArtProject___artist``). |
| 70 |
* Filtering model types: ``instance_of(...)`` and ``not_instance_of(...)`` |
| 71 |
* Combining querysets of different models (``qs3 = qs1 | qs2``) |
| 72 |
* Support for custom user-defined managers. |
| 73 |
* Uses the minimum amount of queries needed to fetch the inherited models. |
| 74 |
* Disabling polymorphic behavior when needed. |
| 75 |
|
| 76 |
|
| 77 |
**Note:** While [django-polymorphic](https://pypi.python.org/pypi/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! |
| 78 |
|
| 79 |
For more information, see the [documentation at Read the Docs](https://django-polymorphic.readthedocs.io). |
| 80 |
|
| 81 |
### Installation |
| 82 |
|
| 83 |
```bash |
| 84 |
$ pip install django-polymorphic |
| 85 |
``` |
| 86 |
|
| 87 |
```python |
| 88 |
INSTALLED_APPS = [ |
| 89 |
... |
| 90 |
"django.contrib.contenttypes", # we rely on the contenttypes framework |
| 91 |
"polymorphic" |
| 92 |
] |
| 93 |
``` |
| 94 |
|
| 95 |
## License |
| 96 |
|
| 97 |
[django-polymorphic](https://pypi.python.org/pypi/django-polymorphic) uses the same license as Django (BSD-like). |