| 1 |
Admin Integration |
| 2 |
================= |
| 3 |
|
| 4 |
Of course, it's possible to register individual polymorphic models in the |
| 5 |
:doc:`Django admin interface <django:ref/contrib/admin/index>`. However, to use these models in a |
| 6 |
single cohesive interface, some extra base classes are available. |
| 7 |
|
| 8 |
Setup |
| 9 |
----- |
| 10 |
|
| 11 |
Both the parent model and child model need to have a :class:`~django.contrib.admin.ModelAdmin` |
| 12 |
class. |
| 13 |
|
| 14 |
The shared base model should use the :class:`~polymorphic.admin.PolymorphicParentModelAdmin` as base |
| 15 |
class. |
| 16 |
|
| 17 |
* :attr:`~polymorphic.admin.PolymorphicParentModelAdmin.base_model` should be set |
| 18 |
* :attr:`~polymorphic.admin.PolymorphicParentModelAdmin.child_models` or |
| 19 |
:meth:`~polymorphic.admin.PolymorphicParentModelAdmin.get_child_models` should return an iterable |
| 20 |
of Model classes. |
| 21 |
|
| 22 |
The admin class for every child model should inherit from |
| 23 |
:class:`~polymorphic.admin.PolymorphicChildModelAdmin` |
| 24 |
|
| 25 |
* :attr:`~polymorphic.admin.PolymorphicChildModelAdmin.base_model` should be set. |
| 26 |
|
| 27 |
Although the child models are registered too, they won't be shown in the admin index page. |
| 28 |
This only happens when :attr:`~polymorphic.admin.PolymorphicChildModelAdmin.show_in_index` is set to |
| 29 |
``True``. |
| 30 |
|
| 31 |
Fieldset configuration |
| 32 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 33 |
|
| 34 |
The parent admin is only used for the list display of models, and for the edit/delete view of |
| 35 |
non-subclassed models. |
| 36 |
|
| 37 |
All other model types are redirected to the edit/delete/history view of the child model admin. |
| 38 |
Hence, the fieldset configuration should be placed on the child admin. |
| 39 |
|
| 40 |
.. tip:: |
| 41 |
|
| 42 |
When the child admin is used as base class for various derived classes, avoid using |
| 43 |
the standard ``ModelAdmin`` attributes ``form`` and ``fieldsets``. |
| 44 |
Instead, use the ``base_form`` and ``base_fieldsets`` attributes. |
| 45 |
This allows the :class:`~polymorphic.admin.PolymorphicChildModelAdmin` class |
| 46 |
to detect any additional fields in case the child model is overwritten. |
| 47 |
|
| 48 |
.. versionchanged:: 1.0 |
| 49 |
|
| 50 |
It's now needed to register the child model classes too. |
| 51 |
|
| 52 |
In :pypi:`django-polymorphic` 0.9 and below, the |
| 53 |
:meth:`~polymorphic.admin.PolymorphicParentModelAdmin.child_models` was a tuple of a |
| 54 |
(:class:`~django.db.models.Model`, :class:`~polymorphic.admin.PolymorphicChildModelAdmin`). The |
| 55 |
admin classes were registered in an internal class, and kept away from the main admin site. This |
| 56 |
caused various subtle problems with the :class:`~django.db.models.ManyToManyField` and related |
| 57 |
field wrappers, which are fixed by registering the child admin classes too. Note that they are |
| 58 |
hidden from the main view, unless |
| 59 |
:attr:`~polymorphic.admin.PolymorphicChildModelAdmin.show_in_index` is set. |
| 60 |
|
| 61 |
.. _admin-example: |
| 62 |
|
| 63 |
Example |
| 64 |
------- |
| 65 |
|
| 66 |
The models are taken from :ref:`advanced-features`. |
| 67 |
|
| 68 |
.. code-block:: python |
| 69 |
|
| 70 |
from django.contrib import admin |
| 71 |
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin, PolymorphicChildModelFilter |
| 72 |
from .models import ModelA, ModelB, ModelC, StandardModel |
| 73 |
|
| 74 |
|
| 75 |
class ModelAChildAdmin(PolymorphicChildModelAdmin): |
| 76 |
""" Base admin class for all child models """ |
| 77 |
base_model = ModelA # Optional, explicitly set here. |
| 78 |
|
| 79 |
# By using these `base_...` attributes instead of the regular ModelAdmin `form` and `fieldsets`, |
| 80 |
# the additional fields of the child models are automatically added to the admin form. |
| 81 |
base_form = ... |
| 82 |
base_fieldsets = ( |
| 83 |
... |
| 84 |
) |
| 85 |
|
| 86 |
|
| 87 |
@admin.register(ModelB) |
| 88 |
class ModelBAdmin(ModelAChildAdmin): |
| 89 |
base_model = ModelB # Explicitly set here! |
| 90 |
# define custom features here |
| 91 |
|
| 92 |
|
| 93 |
@admin.register(ModelC) |
| 94 |
class ModelCAdmin(ModelBAdmin): |
| 95 |
base_model = ModelC # Explicitly set here! |
| 96 |
show_in_index = True # makes child model admin visible in main admin site |
| 97 |
# define custom features here |
| 98 |
|
| 99 |
|
| 100 |
@admin.register(ModelA) |
| 101 |
class ModelAParentAdmin(PolymorphicParentModelAdmin): |
| 102 |
""" The parent model admin """ |
| 103 |
base_model = ModelA # Optional, explicitly set here. |
| 104 |
child_models = (ModelB, ModelC) |
| 105 |
list_filter = (PolymorphicChildModelFilter,) # This is optional. |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
Filtering child types |
| 110 |
--------------------- |
| 111 |
|
| 112 |
Child model types can be filtered by adding a |
| 113 |
:class:`~polymorphic.admin.PolymorphicChildModelFilter` to the |
| 114 |
:attr:`~django.contrib.admin.ModelAdmin.list_filter` attribute. See the example above. |
| 115 |
|
| 116 |
|
| 117 |
Inline models |
| 118 |
------------- |
| 119 |
|
| 120 |
.. versionadded:: 1.0 |
| 121 |
|
| 122 |
Inline models are handled via a special :class:`~polymorphic.admin.StackedPolymorphicInline` class. |
| 123 |
|
| 124 |
For models with a generic foreign key, there is a |
| 125 |
:class:`~polymorphic.admin.GenericStackedPolymorphicInline` class available. |
| 126 |
|
| 127 |
When the inline is included to a normal :class:`~django.contrib.admin.ModelAdmin`, make sure the |
| 128 |
:class:`~polymorphic.admin.PolymorphicInlineSupportMixin` is included. This is not needed when the |
| 129 |
admin inherits from the :class:`~polymorphic.admin.PolymorphicParentModelAdmin` or |
| 130 |
:class:`~polymorphic.admin.PolymorphicChildModelAdmin` classes. |
| 131 |
|
| 132 |
In the following example, the ``PaymentInline`` supports several types. These are defined as |
| 133 |
separate inline classes. The child classes can be nested for clarity, but this is not a requirement. |
| 134 |
|
| 135 |
.. code-block:: python |
| 136 |
|
| 137 |
from django.contrib import admin |
| 138 |
|
| 139 |
from polymorphic.admin import PolymorphicInlineSupportMixin, StackedPolymorphicInline |
| 140 |
from .models import Order, Payment, CreditCardPayment, BankPayment, SepaPayment |
| 141 |
|
| 142 |
|
| 143 |
class PaymentInline(StackedPolymorphicInline): |
| 144 |
""" |
| 145 |
An inline for a polymorphic model. |
| 146 |
The actual form appearance of each row is determined by |
| 147 |
the child inline that corresponds with the actual model type. |
| 148 |
""" |
| 149 |
class CreditCardPaymentInline(StackedPolymorphicInline.Child): |
| 150 |
model = CreditCardPayment |
| 151 |
|
| 152 |
class BankPaymentInline(StackedPolymorphicInline.Child): |
| 153 |
model = BankPayment |
| 154 |
|
| 155 |
class SepaPaymentInline(StackedPolymorphicInline.Child): |
| 156 |
model = SepaPayment |
| 157 |
|
| 158 |
model = Payment |
| 159 |
child_inlines = ( |
| 160 |
CreditCardPaymentInline, |
| 161 |
BankPaymentInline, |
| 162 |
SepaPaymentInline, |
| 163 |
) |
| 164 |
|
| 165 |
|
| 166 |
@admin.register(Order) |
| 167 |
class OrderAdmin(PolymorphicInlineSupportMixin, admin.ModelAdmin): |
| 168 |
""" |
| 169 |
Admin for orders. |
| 170 |
The inline is polymorphic. |
| 171 |
To make sure the inlines are properly handled, |
| 172 |
the ``PolymorphicInlineSupportMixin`` is needed to |
| 173 |
""" |
| 174 |
inlines = (PaymentInline,) |
| 175 |
|
| 176 |
|
| 177 |
Using polymorphic models in standard inlines |
| 178 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 179 |
|
| 180 |
To add a polymorphic child model as an Inline for another model, add a field to the inline's |
| 181 |
:attr:`~django.contrib.admin.ModelAdmin.readonly_fields` list formed by the lowercased name of the |
| 182 |
polymorphic parent model with the string ``_ptr`` appended to it. Otherwise, trying to save that |
| 183 |
model in the admin will raise an :exc:`AttributeError` with the message "can't set attribute". |
| 184 |
|
| 185 |
.. code-block:: python |
| 186 |
|
| 187 |
from django.contrib import admin |
| 188 |
from .models import StandardModel |
| 189 |
|
| 190 |
|
| 191 |
class ModelBInline(admin.StackedInline): |
| 192 |
model = ModelB |
| 193 |
fk_name = 'modelb' |
| 194 |
readonly_fields = ['modela_ptr'] |
| 195 |
|
| 196 |
|
| 197 |
@admin.register(StandardModel) |
| 198 |
class StandardModelAdmin(admin.ModelAdmin): |
| 199 |
inlines = [ModelBInline] |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
Internal details |
| 204 |
---------------- |
| 205 |
|
| 206 |
The polymorphic admin interface works in a simple way: |
| 207 |
|
| 208 |
* The add screen gains an additional step where the desired child model is selected. |
| 209 |
* The edit screen displays the admin interface of the child model. |
| 210 |
* The list screen still displays all objects of the base class. |
| 211 |
|
| 212 |
The polymorphic admin is implemented via a parent admin that redirects the ``edit`` and ``delete`` |
| 213 |
views to the :class:`~django.contrib.admin.ModelAdmin` of the derived child model. The ``list`` page |
| 214 |
is still implemented by the parent model admin. |
| 215 |
|
| 216 |
The parent model |
| 217 |
~~~~~~~~~~~~~~~~ |
| 218 |
|
| 219 |
The parent model needs to inherit :class:`~polymorphic.admin.PolymorphicParentModelAdmin`, and |
| 220 |
implement the following: |
| 221 |
|
| 222 |
* :attr:`~polymorphic.admin.PolymorphicParentModelAdmin.base_model` should be set |
| 223 |
* :attr:`~polymorphic.admin.PolymorphicParentModelAdmin.child_models` or |
| 224 |
:meth:`~polymorphic.admin.PolymorphicParentModelAdmin.get_child_models` should return an iterable |
| 225 |
of Model classes. |
| 226 |
|
| 227 |
The exact implementation can depend on the way your module is structured. For simple inheritance |
| 228 |
situations, :meth:`~polymorphic.admin.PolymorphicParentModelAdmin.child_models` is the best |
| 229 |
solution. For large applications, |
| 230 |
:meth:`~polymorphic.admin.PolymorphicParentModelAdmin.get_child_models` can be used to query a |
| 231 |
plugin registration system. |
| 232 |
|
| 233 |
By default, the :meth:`~polymorphic.managers.PolymorphicQuerySet.non_polymorphic` method will be |
| 234 |
called on the queryset, so only the Parent model will be provided to the list template. This is to |
| 235 |
avoid the performance hit of retrieving child models. |
| 236 |
|
| 237 |
This can be controlled by setting the |
| 238 |
:attr:`~polymorphic.admin.PolymorphicParentModelAdmin.polymorphic_list` property on the parent |
| 239 |
admin. Setting it to True will provide child models to the list template. |
| 240 |
|
| 241 |
If you use other applications such as django-reversion_ or django-mptt_, please check |
| 242 |
:ref:`integrations`. |
| 243 |
|
| 244 |
Note: If you are using non-integer primary keys in your model, you have to edit |
| 245 |
:attr:`~polymorphic.admin.PolymorphicParentModelAdmin.pk_regex`, for example |
| 246 |
``pk_regex = '([\w-]+)'`` if you use :class:`~uuid.UUID` primary keys. Otherwise you cannot change |
| 247 |
model entries. |
| 248 |
|
| 249 |
The child models |
| 250 |
~~~~~~~~~~~~~~~~ |
| 251 |
|
| 252 |
The admin interface of the derived models should inherit from |
| 253 |
:class:`~polymorphic.admin.PolymorphicChildModelAdmin`. Again, |
| 254 |
:attr:`~polymorphic.admin.PolymorphicChildModelAdmin.base_model` should be set in this class as |
| 255 |
well. This class implements the following features: |
| 256 |
|
| 257 |
* It corrects the breadcrumbs in the admin pages. |
| 258 |
* It extends the template lookup paths, to look for both the parent model and child model in the |
| 259 |
``admin/app/model/change_form.html`` path. |
| 260 |
* It allows to set :attr:`~polymorphic.admin.PolymorphicChildModelAdmin.base_form` so the derived |
| 261 |
class will automatically include other fields in the form. |
| 262 |
* It allows to set :attr:`~polymorphic.admin.PolymorphicChildModelAdmin.base_fieldsets` so the |
| 263 |
derived class will automatically display any extra fields. |
| 264 |
* Although it must be registered with admin site, by default it's hidden from admin site index page. |
| 265 |
This can be overridden by adding |
| 266 |
:attr:`~polymorphic.admin.PolymorphicChildModelAdmin.show_in_index` = ``True`` in admin class. |
| 267 |
|
| 268 |
|
| 269 |
.. _django-reversion: https://github.com/etianen/django-reversion |
| 270 |
.. _django-mptt: https://github.com/django-mptt/django-mptt |