ModelAdmin classes

The PolymorphicParentModelAdmin class

class polymorphic.admin.PolymorphicParentModelAdmin(model, admin_site, *args, **kwargs)

Bases: django.contrib.admin.options.ModelAdmin

A admin interface that can displays different change/delete pages, depending on the polymorphic model. To use this class, one attribute need to be defined:

Alternatively, the following methods can be implemented:

This class needs to be inherited by the model admin base class that is registered in the site. The derived models should not register the ModelAdmin, but instead it should be returned by get_child_models().


alias of polymorphic.admin.forms.PolymorphicModelChoiceForm

__init__(model, admin_site, *args, **kwargs)

Initialize self. See help(type(self)) for accurate signature.

add_type_view(request, form_url='')

Display a choice form to select which page type to add.

add_view(request, form_url='', extra_context=None)

Redirect the add view to the real admin.

change_view(request, object_id, *args, **kwargs)

Redirect the change view to the real admin.

changeform_view(request, object_id=None, *args, **kwargs)
delete_view(request, object_id, extra_context=None)

Redirect the delete view to the real admin.


Return the derived model classes which this admin should handle. This should return a list of tuples, exactly like child_models is.

The model classes can be retrieved as base_model.__subclasses__(), a setting in a config file, or a query of a plugin registration system at your option

get_child_type_choices(request, action)

Return a list of polymorphic types for which the user has the permission to perform the given action.


Return the preserved filters querystring.


Return a QuerySet of all model instances that can be edited by the admin site. This is used by changelist_view.


Expose the custom URLs for the subclasses and the URL resolver.

history_view(request, object_id, extra_context=None)

Redirect the history view to the real admin.

register_child(model, model_admin)

Register a model with admin to display.

render_add_type_form(request, context, form_url='')

Render the page type choice form.

subclass_view(request, path)

Forward any request to a custom view of the real admin.

add_type_template = None
base_model = None

The base model that the class uses (auto-detected if not set explicitly)

child_models = None

The child models that should be displayed

pk_regex = '(\\d+|__fk__)'

The regular expression to filter the primary key in the URL. This accepts only numbers as defensive measure against catch-all URLs. If your primary key consists of string values, update this regular expression.

polymorphic_list = False

Whether the list should be polymorphic too, leave to False to optimize

The PolymorphicChildModelAdmin class

class polymorphic.admin.PolymorphicChildModelAdmin(model, admin_site, *args, **kwargs)

Bases: django.contrib.admin.options.ModelAdmin

The optional base class for the admin interface of derived models.

This base class defines some convenience behavior for the admin interface:

  • It corrects the breadcrumbs in the admin pages.
  • It adds the base model to the template lookup paths.
  • It allows to set base_form so the derived class will automatically include other fields in the form.
  • It allows to set base_fieldsets so the derived class will automatically display any extra fields.
__init__(model, admin_site, *args, **kwargs)

Initialize self. See help(type(self)) for accurate signature.

delete_view(request, object_id, context=None)
get_base_fieldsets(request, obj=None)
get_fieldsets(request, obj=None)

Hook for specifying fieldsets.

get_form(request, obj=None, **kwargs)

Return a Form class for use in the admin add view. This is used by add_view and change_view.


Return a dict of all perms for this model. This dict has the keys add, change, delete, and view mapping to the True/False for each of those actions.

get_subclass_fields(request, obj=None)
history_view(request, object_id, extra_context=None)

The ‘history’ admin view for this model.

render_change_form(request, context, add=False, change=False, form_url='', obj=None)
response_post_save_add(request, obj)

Figure out where to redirect after the ‘Save’ button has been pressed when adding a new object.

response_post_save_change(request, obj)

Figure out where to redirect after the ‘Save’ button has been pressed when editing an existing object.

base_fieldsets = None

By setting base_fieldsets instead of fieldsets, any subclass fields can be automatically added. This is useful when your model admin class is inherited by others.

base_form = None

By setting base_form instead of form, any subclass fields are automatically added to the form. This is useful when your model admin class is inherited by others.

base_model = None

The base model that the class uses (auto-detected if not set explicitly)

extra_fieldset_title = 'Contents'

Default title for extra fieldset

show_in_index = False

Whether the child admin model should be visible in the admin index page.

List filtering

The PolymorphicChildModelFilter class

class polymorphic.admin.PolymorphicChildModelFilter(request, params, model, model_admin)

Bases: django.contrib.admin.filters.SimpleListFilter

An admin list filter for the PolymorphicParentModelAdmin which enables filtering by its child models.

This can be used in the parent admin:

list_filter = (PolymorphicChildModelFilter,)

Inlines support

The StackedPolymorphicInline class

class polymorphic.admin.StackedPolymorphicInline(parent_model, admin_site)

Bases: polymorphic.admin.inlines.PolymorphicInlineModelAdmin

Stacked inline for django-polymorphic models. Since tabular doesn’t make much sense with changed fields, just offer this one.

The GenericStackedPolymorphicInline class

class polymorphic.admin.GenericStackedPolymorphicInline(parent_model, admin_site)

Bases: polymorphic.admin.generic.GenericPolymorphicInlineModelAdmin

The stacked layout for generic inlines.

template = 'admin/polymorphic/edit_inline/stacked.html'

The default template to use.

The PolymorphicInlineSupportMixin class

class polymorphic.admin.PolymorphicInlineSupportMixin

Bases: object

A Mixin to add to the regular admin, so it can work with our polymorphic inlines.

This mixin needs to be included in the admin that hosts the inlines. It makes sure the generated admin forms have different fieldsets/fields depending on the polymorphic type of the form instance.

This is achieved by overwriting get_inline_formsets() to return an PolymorphicInlineAdminFormSet instead of a standard Django InlineAdminFormSet for the polymorphic formsets.

get_inline_formsets(request, formsets, inline_instances, obj=None, *args, **kwargs)

Overwritten version to produce the proper admin wrapping for the polymorphic inline formset. This fixes the media and form appearance of the inline polymorphic models.

Low-level classes

These classes are useful when existing parts of the admin classes.

class polymorphic.admin.PolymorphicModelChoiceForm(*args, **kwargs)

Bases: django.forms.forms.Form

The default form for the add_type_form. Can be overwritten and replaced.

Form fields:

  • ct_id: Type (ChoiceField)
__init__(*args, **kwargs)

Initialize self. See help(type(self)) for accurate signature.

type_label = 'Type'

Define the label for the radiofield

class polymorphic.admin.PolymorphicInlineModelAdmin(parent_model, admin_site)

Bases: django.contrib.admin.options.InlineModelAdmin

A polymorphic inline, where each formset row can be a different form.

Note that:

  • Permissions are only checked on the base model.
  • The child inlines can’t override the base model fields, only this parent inline can do that.
class Child(parent_inline)

Bases: django.contrib.admin.options.InlineModelAdmin

The child inline; which allows configuring the admin options for the child appearance.

Note that not all options will be honored by the parent, notably the formset options: * extra * min_num * max_num

The model form options however, will all be read.


alias of polymorphic.formsets.models.PolymorphicFormSetChild


Initialize self. See help(type(self)) for accurate signature.

get_fields(request, obj=None)

Hook for specifying fields.

get_formset(request, obj=None, **kwargs)

Return a BaseInlineFormSet class for use in admin add/change views.

get_formset_child(request, obj=None, **kwargs)

Return the formset child that the parent inline can use to represent us.

Return type:PolymorphicFormSetChild
extra = 0

alias of polymorphic.formsets.models.BasePolymorphicInlineFormSet

__init__(parent_model, admin_site)

Initialize self. See help(type(self)) for accurate signature.


Find the child inline for a given model.

Return type:PolymorphicInlineModelAdmin.Child

:rtype List[PolymorphicInlineModelAdmin.Child]

get_fields(request, obj=None)

Hook for specifying fields.

get_fieldsets(request, obj=None)

Hook for specifying fieldsets.

get_formset(request, obj=None, **kwargs)

Construct the inline formset class.

This passes all class attributes to the formset.

Return type:type
get_formset_children(request, obj=None)

The formset ‘children’ provide the details for all child models that are part of this formset. It provides a stripped version of the modelform/formset factory methods.

child_inlines = ()

Inlines for all model sub types that can be displayed in this inline. Each row is a PolymorphicInlineModelAdmin.Child

extra = 0

The extra forms to show By default there are no ‘extra’ forms as the desired type is unknown. Instead, add each new item using JavaScript that first offers a type-selection.

polymorphic_media = Media(css={'all': ['polymorphic/css/polymorphic_inlines.css']}, js=['admin/js/vendor/jquery/jquery.min.js', 'admin/js/jquery.init.js', 'polymorphic/js/polymorphic_inlines.js'])

The extra media to add for the polymorphic inlines effect. This can be redefined for subclasses.

class polymorphic.admin.GenericPolymorphicInlineModelAdmin(parent_model, admin_site)

Bases: polymorphic.admin.inlines.PolymorphicInlineModelAdmin, django.contrib.contenttypes.admin.GenericInlineModelAdmin

Base class for variation of inlines based on generic foreign keys.

class Child(parent_inline)

Bases: polymorphic.admin.inlines.Child

Variation for generic inlines.


alias of polymorphic.formsets.generic.GenericPolymorphicFormSetChild

get_formset_child(request, obj=None, **kwargs)

Return the formset child that the parent inline can use to represent us.

Return type:PolymorphicFormSetChild

Expose the ContentType that the child relates to. This can be used for the polymorphic_ctype field.

ct_field = 'content_type'
ct_fk_field = 'object_id'

alias of polymorphic.formsets.generic.BaseGenericPolymorphicInlineFormSet

get_formset(request, obj=None, **kwargs)

Construct the generic inline formset class.

class polymorphic.admin.PolymorphicInlineAdminForm(formset, form, fieldsets, prepopulated_fields, original, readonly_fields=None, model_admin=None, view_on_site_url=None)

Bases: django.contrib.admin.helpers.InlineAdminForm

Expose the admin configuration for a form

class polymorphic.admin.PolymorphicInlineAdminFormSet(*args, **kwargs)

Bases: django.contrib.admin.helpers.InlineAdminFormSet

Internally used class to expose the formset in the template.