From df178dbb4479ba248f55bf3733953244c4973aff Mon Sep 17 00:00:00 2001 From: Vail Gold Date: Sat, 19 Apr 2014 14:28:08 -0400 Subject: [PATCH] Added to admin docs how to add polymorphic child model as inline. --- docs/admin.rst | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/docs/admin.rst b/docs/admin.rst index db84b56..ae8551a 100644 --- a/docs/admin.rst +++ b/docs/admin.rst @@ -53,6 +53,12 @@ use the ``base_form`` and ``base_fieldsets`` instead. The ``PolymorphicChildMode automatically detect the additional fields that the child model has, display those in a separate fieldset. +Polymorphic Inlines +------------------- + +To add a polymorphic child model as an Inline for another model, add a field to the inline's readonly_fields list formed by the lowercased name of the polymorphic parent model with the string "_ptr" appended to it. Otherwise, trying to save that model in the admin will raise an AttributeError with the message "can't set attribute". + + .. _admin-example: Example @@ -64,7 +70,7 @@ The models are taken from :ref:`advanced-features`. from django.contrib import admin from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin - from .models import ModelA, ModelB, ModelC + from .models import ModelA, ModelB, ModelC, StandardModel class ModelAChildAdmin(PolymorphicChildModelAdmin): @@ -78,9 +84,11 @@ The models are taken from :ref:`advanced-features`. ... ) + class ModelBAdmin(ModelAChildAdmin): # define custom features here + class ModelCAdmin(ModelBAdmin): # define custom features here @@ -93,5 +101,17 @@ The models are taken from :ref:`advanced-features`. (ModelC, ModelCAdmin), ) + + class ModelBInline(admin.StackedInline): + model = ModelB + fk_name = 'modelb' + readonly_fields = ['modela_ptr'] + + + class StandardModelAdmin(admin.ModelAdmin): + inlines = [ModelBInline] + + # Only the parent needs to be registered: admin.site.register(ModelA, ModelAParentAdmin) + admin.site.register(StandardModel, StandardModelAdmin)