I'm trying to figure out a way to display the following RelativeInline only if Person.is_member is True.
Current admin.py:
class RelativeInline(admin.TabularInline):
model = Relative
fk_name = 'member'
class PersonAdmin(admin.ModelAdmin):
inlines = [RelativeInline,]
ordering = ('first_name',)
list_filter = ('is_member',)
search_fields = ('first_name', 'last_name',)
date_hierarchy = 'member_date'
list_display = ('first_name', 'last_name', 'is_member', 'member_date', 'photo')
admin.site.register(Person, PersonAdmin)
The only hint I've been able to find is that I might be able to override get_formset, but I couldn't find a good example, so my feeble attempt didn't work.
Here's my failed attempt:
class RelativeInline(admin.TabularInline):
model = Relative
fk_name = 'member'
class PersonAdmin(admin.ModelAdmin):
ordering = ('first_name',)
list_filter = ('is_member',)
search_fields = ('first_name', 'last_name',)
date_hierarchy = 'member_date'
list_display = ('first_name', 'last_name', 'is_member', 'member_date', 'photo')
def get_formset(self, request, obj=None, **kwargs):
if obj.is_member:
inlines = [RelativeInline,]
return super(PersonAdmin, self).get_formset(request, obj, **kwargs)
admin.site.register(Person, PersonAdmin)
There are no errors generated by this code, but no inline appears regardless of whether or not Person.is_member is True or False.
Update: A friend suggested I try changing:
inlines = [RelativeInline,]
to:
self.inlines = [RelativeInline,]
but to no avail. I also tried:
PersonAdmin.inlines = [RelativeInline,]
but the result was the same -- no error, no inline.