Debian Codegraph @

docs/quickstart.rst

raw ◀ ▶
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 Making Your Models Polymorphic
18 ------------------------------
19
20 Use :class:`~polymorphic.models.PolymorphicModel` instead of Django's
21 :class:`~django.db.models.Model`, like so:
22
23 .. code-block:: python
24
25 from polymorphic.models import PolymorphicModel
26
27 class Project(PolymorphicModel):
28 topic = models.CharField(max_length=30)
29
30 class ArtProject(Project):
31 artist = models.CharField(max_length=30)
32
33 class ResearchProject(Project):
34 supervisor = models.CharField(max_length=30)
35
36 All models inheriting from your polymorphic models will be polymorphic as well.
37
38 Using Polymorphic Models
39 ------------------------
40
41 Create some objects:
42
43 .. code-block:: python
44
45 >>> Project.objects.create(topic="Department Party")
46 >>> ArtProject.objects.create(topic="Painting with Tim", artist="T. Turner")
47 >>> ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
48
49 Get polymorphic query results:
50
51 .. code-block:: python
52
53 >>> Project.objects.all()
54 [ <Project: id 1, topic "Department Party">,
55 <ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">,
56 <ResearchProject: id 3, topic "Swallow Aerodynamics", supervisor "Dr. Winter"> ]
57
58 Use :meth:`~polymorphic.managers.PolymorphicQuerySet.instance_of` and
59 :meth:`~polymorphic.managers.PolymorphicQuerySet.not_instance_of` for narrowing the result to
60 specific subtypes:
61
62 .. code-block:: python
63
64 >>> Project.objects.instance_of(ArtProject)
65 [ <ArtProject: id 2, topic "Painting with Tim", artist "T. Turner"> ]
66
67 .. code-block:: python
68
69 >>> Project.objects.instance_of(ArtProject) | Project.objects.instance_of(ResearchProject)
70 [ <ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">,
71 <ResearchProject: id 3, topic "Swallow Aerodynamics", supervisor "Dr. Winter"> ]
72
73 Polymorphic filtering: Get all projects where Mr. Turner is involved as an artist
74 or supervisor (note the three underscores):
75
76 .. code-block:: python
77
78 >>> Project.objects.filter(Q(ArtProject___artist='T. Turner') | Q(ResearchProject___supervisor='T. Turner'))
79 [ <ArtProject: id 2, topic "Painting with Tim", artist "T. Turner">,
80 <ResearchProject: id 4, topic "Color Use in Late Cubism", supervisor "T. Turner"> ]
81
82 This is basically all you need to know, as *django-polymorphic* mostly
83 works fully automatic and just delivers the expected results.
84
85 .. note::
86 While :pypi:`django-polymorphic` makes subclassed models easy to use in Django,
87 we still encourage to use them with caution. Each subclassed model will require
88 Django to perform an ``INNER JOIN`` to fetch the model fields from the database.
89 While taking this in mind, there are valid reasons for using subclassed models.
90 That's what this library is designed for!