Debian Codegraph @

docs/quickstart.rst

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 Quickstart
2 ===========
3
4 Install the project using::
5
6 pip install django-polymorphic
7
8 Update the settings file:
9
10 .. code-block:: python
11
12 INSTALLED_APPS += (
13 'polymorphic',
14 'django.contrib.contenttypes',
15 )
16
17 .. only:: html
18
19 The current release of :pypi:`django-polymorphic` supports:
20
21 .. image:: https://badge.fury.io/py/django-polymorphic.svg
22 :target: https://pypi.python.org/pypi/django-polymorphic/
23 :alt: PyPI version
24
25 .. image:: https://img.shields.io/pypi/pyversions/django-polymorphic.svg
26 :target: https://pypi.python.org/pypi/django-polymorphic/
27 :alt: Supported Pythons
28
29 .. image:: https://img.shields.io/pypi/djversions/django-polymorphic.svg
30 :target: https://pypi.org/project/django-polymorphic/
31 :alt: Supported Django
32
33
34 Making Your Models Polymorphic
35 ------------------------------
36
37 Use :class:`~polymorphic.models.PolymorphicModel` instead of Django's
38 :class:`~django.db.models.Model`, like so:
39
40 .. code-block:: python
41
42 from polymorphic.models import PolymorphicModel
43
44 class Project(PolymorphicModel):
45 topic = models.CharField(max_length=30)
46
47 class ArtProject(Project):
48 artist = models.CharField(max_length=30)
49
50 class ResearchProject(Project):
51 supervisor = models.CharField(max_length=30)
52
53 All models inheriting from your polymorphic models will be polymorphic as well.
54
55 Using Polymorphic Models
56 ------------------------
57
58 Create some objects:
59
60 .. code-block:: python
61
62 >>> Project.objects.create(topic="Department Party")
63 >>> ArtProject.objects.create(topic="Painting with Tim", artist="T. Turner")
64 >>> ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
65
66 Get polymorphic query results:
67
68 .. code-block:: python
69
70 >>> Project.objects.all()
71 [ <Project: id 1, topic "Department Party">,
72 <ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">,
73 <ResearchProject: id 3, topic "Swallow Aerodynamics", supervisor "Dr. Winter"> ]
74
75 Use :meth:`~polymorphic.managers.PolymorphicQuerySet.instance_of` and
76 :meth:`~polymorphic.managers.PolymorphicQuerySet.not_instance_of` for narrowing the result to
77 specific subtypes:
78
79 .. code-block:: python
80
81 >>> Project.objects.instance_of(ArtProject)
82 [ <ArtProject: id 2, topic "Painting with Tim", artist "T. Turner"> ]
83
84 .. code-block:: python
85
86 >>> Project.objects.instance_of(ArtProject) | Project.objects.instance_of(ResearchProject)
87 [ <ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">,
88 <ResearchProject: id 3, topic "Swallow Aerodynamics", supervisor "Dr. Winter"> ]
89
90 Polymorphic filtering: Get all projects where Mr. Turner is involved as an artist
91 or supervisor (note the three underscores):
92
93 .. code-block:: python
94
95 >>> Project.objects.filter(Q(ArtProject___artist='T. Turner') | Q(ResearchProject___supervisor='T. Turner'))
96 [ <ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">,
97 <ResearchProject: id 4, topic "Color Use in Late Cubism", supervisor "T. Turner"> ]
98
99 This is basically all you need to know, as *django-polymorphic* mostly
100 works fully automatic and just delivers the expected results.
101
102 .. note::
103 While :pypi:`django-polymorphic` makes subclassed models easy to use in Django,
104 we still encourage to use them with caution. Each subclassed model will require
105 Django to perform an ``INNER JOIN`` to fetch the model fields from the database.
106 While taking this in mind, there are valid reasons for using subclassed models.
107 That's what this library is designed for!