Debian Codegraph @

README.md

raw ◀ ▶
1 Debian patch touch this file
1 # django-polymorphic
2
3 ---------------------------------------------------------------------------------------------------
4
5 ## Polymorphic Models for Django
6
7 [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.
8
9 When we store models that inherit from a ``Project`` model...
10
11 ```python
12
13 >>> Project.objects.create(topic="Department Party")
14 >>> ArtProject.objects.create(topic="Painting with Tim", artist="T. Turner")
15 >>> ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
16 ```
17
18 ...and want to retrieve all our projects, the subclassed models are returned!
19
20 ```python
21
22 >>> Project.objects.all()
23 [ <Project: id 1, topic "Department Party">,
24 <ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">,
25 <ResearchProject: id 3, topic "Swallow Aerodynamics", supervisor "Dr. Winter"> ]
26 ```
27
28 Using vanilla Django, we get the base class objects, which is rarely what we wanted:
29
30 ```python
31
32 >>> Project.objects.all()
33 [ <Project: id 1, topic "Department Party">,
34 <Project: id 2, topic "Painting with Tim">,
35 <Project: id 3, topic "Swallow Aerodynamics"> ]
36 ```
37
38 This also works when the polymorphic model is accessed via
39 ForeignKeys, ManyToManyFields or OneToOneFields.
40
41 ### Features
42
43 * Full admin integration.
44 * ORM integration:
45
46 * support for ForeignKey, ManyToManyField, OneToOneField descriptors.
47 * Filtering/ordering of inherited models (``ArtProject___artist``).
48 * Filtering model types: ``instance_of(...)`` and ``not_instance_of(...)``
49 * Combining querysets of different models (``qs3 = qs1 | qs2``)
50 * Support for custom user-defined managers.
51 * Uses the minimum amount of queries needed to fetch the inherited models.
52 * Disabling polymorphic behavior when needed.
53
54
55 **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!
56
57 For more information, see the [documentation at Read the Docs](https://django-polymorphic.readthedocs.io).
58
59 ### Installation
60
61 ```bash
62 $ pip install django-polymorphic
63 ```
64
65 ```python
66 INSTALLED_APPS = [
67 ...
68 "django.contrib.contenttypes", # we rely on the contenttypes framework
69 "polymorphic"
70 ]
71 ```
72
73 ## License
74
75 [django-polymorphic](https://pypi.python.org/pypi/django-polymorphic) uses the same license as Django (BSD-like).