Debian Codegraph @

docs/integrations/index.rst

raw
1 .. _integrations:
2
3 Integrations
4 ============
5
6 When integrating polymorphic models into third party apps you have three primary options:
7
8 0. Hope it just works (it might!).
9 1. Ensure the querysets the third party apps see are
10 :meth:`not polymorphic <polymorphic.query.PolymorphicQuerySet.non_polymorphic>`.
11 2. Override or extend relevant third party app code to work with polymorphic querysets.
12
13 If it does not just work, option 1 is usually the easiest. We provide some integrations in
14 :mod:`polymorphic.contrib` for popular third party apps and provide guidance for others below.
15
16 This page does not exhaustively cover all integrations. If you feel your integration need is
17 very common you may consider opening a PR to either provide support in code or documentation here.
18
19 This page covers supported and tested integration advice. For all other integration advice please
20 refer to `our integrations discussion page
21 <https://github.com/jazzband/django-polymorphic/discussions/categories/integrations>`_.
22
23 For the integration examples on this page, we use the following polymorphic model hierarchy:
24
25 .. literalinclude:: ../../src/polymorphic/tests/examples/integrations/models.py
26 :language: python
27 :linenos:
28
29
30 .. _django-django-guardian-support:
31
32 :pypi:`django-guardian`
33 -----------------------
34
35 .. versionadded:: 1.0.2
36
37 No special modifications are required to integrate with :pypi:`django-guardian`. However, if you
38 would like all object level permissions to be managed at the base model level, rather than have
39 unique permissions for each polymorphic subclass, then you can use the helper function
40 :func:`polymorphic.contrib.guardian.get_polymorphic_base_content_type` to unify the permissions
41 for your entire polymorphic model tree into a single namespace a the base level:
42
43 .. code-block:: python
44
45 GUARDIAN_GET_CONTENT_TYPE = \
46 "polymorphic.contrib.guardian.get_polymorphic_base_content_type"
47
48 This option requires :pypi:`django-guardian` >= 1.4.6. Details about how this option works are
49 available in the `django-guardian documentation
50 <https://django-guardian.readthedocs.io/en/stable/configuration>`_.
51
52 .. _django-extra-views-support:
53
54 :pypi:`django-extra-views`
55 --------------------------
56
57 .. versionadded:: 1.1
58
59 The :mod:`polymorphic.contrib.extra_views` package provides classes to display polymorphic formsets
60 using the classes from :pypi:`django-extra-views`. See the documentation of:
61
62 * :class:`~polymorphic.contrib.extra_views.PolymorphicFormSetView`
63 * :class:`~polymorphic.contrib.extra_views.PolymorphicInlineFormSetView`
64 * :class:`~polymorphic.contrib.extra_views.PolymorphicInlineFormSet`
65
66 .. tip::
67
68 The complete working code for this example can be found `in the extra_views integration test
69 <https://github.com/jazzband/django-polymorphic/tree/HEAD/src/polymorphic/tests/examples/integrations/extra_views>`_.
70
71
72 Example View
73 ~~~~~~~~~~~~
74
75 Here's how to create a view using :class:`~polymorphic.contrib.extra_views.PolymorphicFormSetView`
76 to handle polymorphic formsets:
77
78 .. literalinclude:: ../../src/polymorphic/tests/examples/integrations/extra_views/views.py
79 :language: python
80 :linenos:
81
82
83 URL Configuration
84 ~~~~~~~~~~~~~~~~~
85
86 Configure the URL patterns to route to your formset view:
87
88 .. literalinclude:: ../../src/polymorphic/tests/examples/integrations/extra_views/urls.py
89 :language: python
90 :linenos:
91
92
93 Template
94 ~~~~~~~~
95
96 The template for rendering the formset:
97
98 .. literalinclude:: ../../src/polymorphic/tests/examples/integrations/extra_views/templates/extra_views/article_formset.html
99 :language: html+django
100
101 ``model_name`` is a template tag implemented like so:
102
103 .. literalinclude:: ../../src/polymorphic/tests/examples/integrations/extra_views/templatetags/extra_views_tags.py
104 :language: python
105 :lines: 6-
106
107 .. _django-reversion-support:
108
109 :pypi:`django-reversion`
110 ------------------------
111
112 Support for :pypi:`django-reversion` works as expected with polymorphic models. We just need to
113 do two things:
114
115 1. Inherit our admin classes from both :class:`~polymorphic.admin.PolymorphicParentModelAdmin` /
116 :class:`~polymorphic.admin.PolymorphicChildModelAdmin` and
117 :ref:`VersionAdmin <django-reversion:versionadmin>`.
118 2. Override the ``admin/polymorphic/object_history.html`` template.
119
120 .. tip::
121
122 The complete working code for this example can be found `in the reversion integration test
123 <https://github.com/jazzband/django-polymorphic/tree/HEAD/src/polymorphic/tests/examples/integrations/reversion>`_.
124
125
126 Admin Configuration
127 ~~~~~~~~~~~~~~~~~~~
128
129 The admin configuration combines :class:`~polymorphic.admin.PolymorphicParentModelAdmin` and
130 :class:`~polymorphic.admin.PolymorphicChildModelAdmin` with
131 :ref:`VersionAdmin <django-reversion:versionadmin>`:
132
133 .. literalinclude:: ../../src/polymorphic/tests/examples/integrations/reversion/admin.py
134 :language: python
135 :linenos:
136
137
138 Custom Template
139 ~~~~~~~~~~~~~~~
140
141 Since both :class:`~polymorphic.admin.PolymorphicParentModelAdmin` and
142 :ref:`VersionAdmin <django-reversion:versionadmin>`. define ``object_history.html`` template, you
143 need to create a custom template that combines both:
144
145 .. literalinclude:: ../../src/polymorphic/tests/examples/integrations/reversion/templates/admin/polymorphic/object_history.html
146 :language: html+django
147
148 This makes sure both the reversion template is used, and the breadcrumb is corrected for the
149 polymorphic model using the :templatetag:`breadcrumb_scope`
150 tag.
151
152 .. _model-bakery:
153
154 :pypi:`model-bakery`
155 --------------------
156
157 :pypi:`model-bakery` does not work without without special configuration for polymorphic models
158 because it overrides the :attr:`~polymorphic.models.PolymorphicModel.polymorphic_ctype` field.
159 The best option to make it work in all cases is to `supply a custom Baker
160 <https://model-bakery.readthedocs.io/en/latest/how_bakery_behaves.html#customizing-baker>`_ class
161 that fills in all fields except :attr:`~polymorphic.models.PolymorphicModel.polymorphic_ctype`:
162
163 .. code-block:: python
164 :linenos:
165 :caption: yoursite/tests/baker.py
166
167 from polymorphic.models import PolymorphicModel
168 from model_bakery import baker
169
170
171 class PolymorphicAwareBaker(baker.Baker):
172 """
173 Our custom model baker ignores the polymorphic_ctype field on all polymorphic
174 models - this allows the base class to set it correctly.
175 See https://github.com/python/pythondotorg/issues/2567
176 """
177
178 def get_fields(self):
179 fields = super().get_fields()
180 if issubclass(self.model, PolymorphicModel):
181 fields = {
182 field
183 for field in fields
184 if field.name != "polymorphic_ctype"
185 }
186 return fields
187
188
189 Then in your test settings file:
190
191 .. code-block:: python
192
193 BAKER_CUSTOM_CLASS = "yoursite.tests.baker.PolymorphicAwareBaker"
194
195 You may also simply pass the correct :class:`~django.contrib.contenttypes.models.ContentType`
196 instance to the :attr:`~polymorphic.models.PolymorphicModel.polymorphic_ctype` field when creating
197 polymorphic model instances with ``make()``
198
199
200 Other Integrations
201 ------------------
202
203 .. toctree::
204 :maxdepth: 1
205 :titlesonly:
206
207 drf