@@ -26,10 +26,11 @@ For the integration examples on this page, we use the following polymorphic mode
2626 :language: python
2727 :linenos:
2828
29+
2930.. _django-django-guardian-support :
3031
31- django-guardian
32- ---------------
32+ :pypi: ` django-guardian `
33+ -----------------------
3334
3435.. versionadded :: 1.0.2
3536
@@ -50,8 +51,8 @@ available in the `django-guardian documentation
5051
5152.. _django-extra-views-support :
5253
53- django-extra-views
54- ------------------
54+ :pypi: ` django-extra-views `
55+ --------------------------
5556
5657.. versionadded :: 1.1
5758
@@ -105,8 +106,8 @@ The template for rendering the formset:
105106
106107.. _django-reversion-support :
107108
108- django-reversion
109- ----------------
109+ :pypi: ` django-reversion `
110+ ------------------------
110111
111112Support for :pypi: `django-reversion ` works as expected with polymorphic models. We just need to
112113do two things:
@@ -148,8 +149,55 @@ This makes sure both the reversion template is used, and the breadcrumb is corre
148149polymorphic model using the :templatetag: `breadcrumb_scope `
149150tag.
150151
152+ .. _model-bakery :
151153
152- .. toctree ::
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 = {field for field in fields if field.name != " polymorphic_ctype" }
182+ return fields
153183
154- djangorestframework
155184
185+ Then in your test settings file:
186+
187+ .. code-block :: python
188+
189+ BAKER_CUSTOM_CLASS = " yoursite.tests.baker.PolymorphicAwareBaker"
190+
191+ You may also simply pass the correct :class: `~django.contrib.contenttypes.models.ContentType `
192+ instance to the :attr: `~polymorphic.models.PolymorphicModel.polymorphic_ctype ` field when creating
193+ polymorphic model instances with ``make() ``
194+
195+
196+ Other Integrations
197+ ------------------
198+
199+ .. toctree ::
200+ :maxdepth: 1
201+ :titlesonly:
202+
203+ drf
0 commit comments