I have an admin model with a few inline models included with it (see the ResourceUserAdmin model below for full class):
inlines = [
ResourceLocationInlineAdmin ,
ResourceCategoryInlineAdmin ,
ResourceStageInlineAdmin ,
]
When a user clicks to create a new ResourceUserAdmin I want the inlines of the class ResourceCategoryInlineAdmin to get initial values -- note that these relationships will not be saved to the database. I've tried to override parts of the add_view function to get what i want but I can't figure out how to pass multiple inline forms back to parent.
Any ideas on how to achieve this?
Models
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 )
Here is the inline model I want to create many by default:
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 )