Debian Codegraph @

README.md

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # django-polymorphic
2
3 [![License: BSD](https://img.shields.io/badge/License-BSD-blue.svg)](https://opensource.org/license/bsd-3-clause)
4 [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
5 [![PyPI version](https://badge.fury.io/py/django-polymorphic.svg)](https://pypi.python.org/pypi/django-polymorphic/)
6 [![PyPI pyversions](https://img.shields.io/pypi/pyversions/django-polymorphic.svg)](https://pypi.python.org/pypi/django-polymorphic/)
7 [![PyPI djversions](https://img.shields.io/pypi/djversions/django-polymorphic.svg)](https://pypi.org/project/django-polymorphic/)
8 [![PyPI status](https://img.shields.io/pypi/status/django-polymorphic.svg)](https://pypi.python.org/pypi/django-polymorphic)
9 [![PyPI - Types](https://img.shields.io/pypi/types/django-polymorphic.svg)](https://pypi.python.org/pypi/django-polymorphic)
10 [![Documentation Status](https://readthedocs.org/projects/django-polymorphic/badge/?version=latest)](http://django-polymorphic.readthedocs.io/?badge=latest/)
11 [![Code Cov](https://img.shields.io/codecov/c/github/django-commons/django-polymorphic/main.svg)](https://codecov.io/github/django-commons/django-polymorphic?branch=main)
12 [![Test Status](https://github.com/django-commons/django-polymorphic/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/django-commons/django-polymorphic/actions/workflows/test.yml?query=branch:main)
13 [![Lint Status](https://github.com/django-commons/django-polymorphic/actions/workflows/lint.yml/badge.svg?branch=main)](https://github.com/django-commons/django-polymorphic/actions/workflows/lint.yml?query=branch:main)
14 [![Published on Django Packages](https://img.shields.io/badge/Published%20on-Django%20Packages-0c3c26)](https://djangopackages.org/packages/p/django-polymorphic/)
15 [![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/django-commons/django-polymorphic/badge)](https://securityscorecards.dev/viewer/?uri=github.com/django-commons/django-polymorphic)
16
17 ---------------------------------------------------------------------------------------------------
18
19 [![Postgres](https://img.shields.io/badge/Postgres-12%2B-blue)](https://www.postgresql.org/)
20 [![MySQL](https://img.shields.io/badge/MySQL-8.0%2B-blue)](https://www.mysql.com/)
21 [![MariaDB](https://img.shields.io/badge/MariaDB-10.4%2B-blue)](https://mariadb.org/)
22 [![SQLite](https://img.shields.io/badge/SQLite-3.8%2B-blue)](https://www.sqlite.org/)
23 [![Oracle](https://img.shields.io/badge/Oracle-21c%2B-blue)](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).