Added to admin docs how to add polymorphic child model as inline.
parent
bf64b111c4
commit
df178dbb44
|
|
@ -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.
|
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:
|
.. _admin-example:
|
||||||
|
|
||||||
Example
|
Example
|
||||||
|
|
@ -64,7 +70,7 @@ The models are taken from :ref:`advanced-features`.
|
||||||
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin
|
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin
|
||||||
from .models import ModelA, ModelB, ModelC
|
from .models import ModelA, ModelB, ModelC, StandardModel
|
||||||
|
|
||||||
|
|
||||||
class ModelAChildAdmin(PolymorphicChildModelAdmin):
|
class ModelAChildAdmin(PolymorphicChildModelAdmin):
|
||||||
|
|
@ -78,9 +84,11 @@ The models are taken from :ref:`advanced-features`.
|
||||||
...
|
...
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ModelBAdmin(ModelAChildAdmin):
|
class ModelBAdmin(ModelAChildAdmin):
|
||||||
# define custom features here
|
# define custom features here
|
||||||
|
|
||||||
|
|
||||||
class ModelCAdmin(ModelBAdmin):
|
class ModelCAdmin(ModelBAdmin):
|
||||||
# define custom features here
|
# define custom features here
|
||||||
|
|
||||||
|
|
@ -93,5 +101,17 @@ The models are taken from :ref:`advanced-features`.
|
||||||
(ModelC, ModelCAdmin),
|
(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:
|
# Only the parent needs to be registered:
|
||||||
admin.site.register(ModelA, ModelAParentAdmin)
|
admin.site.register(ModelA, ModelAParentAdmin)
|
||||||
|
admin.site.register(StandardModel, StandardModelAdmin)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue