Skip to content

Commit edc6fd3

Browse files
authored
Merge pull request #849 from bckohan/bakery
Document model-bakery integration and test considerations
2 parents cf367f0 + ddd9cd2 commit edc6fd3

8 files changed

Lines changed: 71 additions & 11 deletions

File tree

docs/advanced.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,12 @@ Restrictions & Caveats
390390
base row. If you must delete manually, ensure you also delete the corresponding row from the
391391
base table.
392392

393+
* There will be problems if the :class:`~django.contrib.contenttypes.models.ContentType` cache
394+
becomes out of sync with the database. This can especially happen in tests. You should ensure
395+
that the cache is cleared (:meth:`~django.contrib.contenttypes.models.ContentTypeManager.clear_cache`)
396+
whenever this happens. In tests this happens when if :django-admin:`flush` is called by the teardown
397+
sequence in :class:`~django.test.TransactionTestCase`.
398+
393399
.. old links:
394400
- http://code.djangoproject.com/wiki/ModelInheritance
395401
- http://lazypython.blogspot.com/2009/02/second-look-at-inheritance-and.html

docs/changelog/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
v4.10.4 (2026-01-19)
5+
--------------------
6+
7+
* Documented `model-bakery integration and test considerations <https://github.com/jazzband/django-polymorphic/issues/849>`_
8+
49
v4.9.1, v4.10.3 (2026-01-15)
510
----------------------------
611

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@
285285
"django-guardian": ("https://django-guardian.readthedocs.io/en/stable", None),
286286
"django-reversion": ("https://django-reversion.readthedocs.io/en/stable", None),
287287
"django-extra-views": ("https://django-extra-views.readthedocs.io/en/stable", None),
288+
"model-bakery": ("https://model-bakery.readthedocs.io/en/stable", None),
288289
}
289290

290291
# autodoc settings
File renamed without changes.

docs/integrations/index.rst

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

111112
Support for :pypi:`django-reversion` works as expected with polymorphic models. We just need to
112113
do two things:
@@ -148,8 +149,55 @@ This makes sure both the reversion template is used, and the breadcrumb is corre
148149
polymorphic model using the :templatetag:`breadcrumb_scope`
149150
tag.
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

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "django-polymorphic"
7-
version = "4.10.3"
7+
version = "4.10.4"
88
description = "Seamless polymorphic inheritance for Django models."
99
readme = "README.md"
1010
license = "BSD-3-Clause"

src/polymorphic/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
Seamless Polymorphic Inheritance for Django Models
2020
"""
2121

22-
VERSION = "4.10.3"
22+
VERSION = "4.10.4"
2323

2424
__title__ = "Django Polymorphic"
2525
__version__ = VERSION # version synonym for backwards compatibility

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)