我有一个管理模型,其中包含一些内联模型(完整类请参见下面的ResourceUserAdmin模型):
inlines = [
ResourceLocationInlineAdmin ,
ResourceCategoryInlineAdmin ,
ResourceStageInlineAdmin ,
]
当用户单击以创建新的ResourceUserAdmin时,我希望ResourceCategoryInlineAdmin 类的内联获取初始值——请注意,这些关系不会保存到数据库中。我试图覆盖add_view函数的部分以获得我想要的,但我不知道如何将多个内联表单传递回父级。
关于如何实现这一目标的任何想法?
楷模
class ResourceUserAdmin( admin.ModelAdmin ):
inlines = [
ResourceLocationInlineAdmin ,
ResourceCategoryInlineAdmin ,
ResourceStageInlineAdmin ,
]
list_display = (
'user' ,
'name' ,
'state' ,
'email' ,
'website' ,
'phone' ,
'logo_url_link',
)
search_fields = ( 'name' , 'email' , 'website' )
list_filter = ( 'name' , 'state' , 'email' , 'website' )
ordering = ( 'name', )
fields = (
'user' ,
'name' ,
'state' ,
'email' ,
'website' ,
'phone' ,
'logo' ,
'ideal_candidate',
)
admin.site.register( ResourceUser, ResourceUserAdmin )
这是我想默认创建的内联模型:
class ResourceCategoryInlineAdmin( admin.StackedInline ):
model = ResourceCategory
extra = 0
class ResourceCategoryAdmin( admin.ModelAdmin ):
list_display = ( 'user' ,
'category' , )
ordering = ( 'user' , )
fields = ( 'user' , 'category' )
def formfield_for_foreignkey( self, db_field, *args, **kwargs ):
if isinstance( db_field, models.ForeignKey ):
if db_field.name == 'category':
kwargs['widget'] = forms.RadioSelect()
return super( ResourceCategoryAdmin, self).formfield_for_foreignkey( db_field, **kwargs )
admin.site.register( ResourceCategory, ResourceCategoryAdmin )