是否可以从 Django类创建子admin.ModelAdmin
类?
我正在尝试做类似的事情:
class PageAdmin(admin.ModelAdmin):
form = PageAdminForm
# Plus many other stuff here (that I don't want to copy/paste in PlacePageAdmin to keep dry...)
class PlacePageAdmin(PageAdmin):
form = PlacePageAdminForm
def get_form(self, request, obj=None, **kwargs):
return super(PlacePageAdmin, self).get_form(request, obj, **kwargs)
def get_fieldsets(self, request, obj=None):
fieldsets = copy.deepcopy(super(PlacePageAdmin, self).get_fieldsets(request, obj))
fieldsets.insert(0,
('Place', {
'fields': (
('address', 'postcode', 'town'),
),
})
)
return fieldsets
def queryset(self, request):
return PlacePage.objects.filter(page_type=Page.PLACE)
但我无法让它工作:u"Key 'address' not found in Form"
尝试访问 PlacePageAdmin 表单页面时出现错误。
有人做过这样的事吗?
非常感谢您!
编辑 - 表格代码:
class PageAdminForm(forms.ModelForm):
extra_field = forms.CharField()
class Meta:
model = Page
class PlacePageAdminForm(PageAdminForm):
class Meta:
model = PlacePage