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, two variables need to be defined:

  • base_model should
  • child_models should be a list of (Model, Admin) tuples

Alternatively, the following methods can be implemented:

  • get_child_models() should return a list of (Model, ModelAdmin) tuples
  • optionally, get_child_type_choices() can be overwritten to refine the choices for the add dialog.

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 PolymorphicModelChoiceForm

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.


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

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)

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.

The base_model attribute must be set.

delete_view(request, object_id, context=None)
get_fieldsets(request, obj=None)
get_form(request, obj=None, **kwargs)
get_subclass_fields(request, obj=None)
history_view(request, object_id, extra_context=None)
render_change_form(request, context, add=False, change=False, form_url='', obj=None)
response_post_save_add(request, obj)
response_post_save_change(request, obj)
base_fieldsets = None
base_form = None
base_model = None
extra_fieldset_title = u'Contents'
show_in_index = False

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)

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.

base_fields = OrderedDict([('ct_id', <django.forms.fields.ChoiceField object>)])
declared_fields = OrderedDict([('ct_id', <django.forms.fields.ChoiceField object>)])
type_label = u'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 PolymorphicFormSetChild

get_fields(request, obj=None)
get_formset(request, obj=None, **kwargs)
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 BasePolymorphicInlineFormSet


Find the child inline for a given model.

Return type:PolymorphicInlineModelAdmin.Child

:rtype List[PolymorphicInlineModelAdmin.Child]

PolymorphicInlineModelAdmin.get_fields(request, obj=None)
PolymorphicInlineModelAdmin.get_fieldsets(request, obj=None)

Hook for specifying fieldsets.

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

Construct the inline formset class.

This passes all class attributes to the formset.

Return type:type
PolymorphicInlineModelAdmin.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.

PolymorphicInlineModelAdmin.child_inlines = ()

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

PolymorphicInlineModelAdmin.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.

PolymorphicInlineModelAdmin.polymorphic_media = <django.forms.widgets.Media object>

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 GenericPolymorphicFormSetChild

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

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'

The formset class

alias of BaseGenericPolymorphicInlineFormSet

GenericPolymorphicInlineModelAdmin.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.