Given a model named MainModel
and a RelatedModel
, where the later has a ForeignKey
field to MainModel
:
class
You need just simply override change_view
in ModelAdmin:
def change_view(self, request, object_id, form_url='', extra_context=None):
obj = self.model.objects.filter(pk=object_id).first()
if not obj:
self.inlines = []
else:
if obj.type is True:
self.inlines = [RelatedModel1InlineAdmin]
else:
self.inlines = [RelatedModel2InlineAdmin]
return super().change_view(request,object_id,form_url=form_url,extra_context=extra_context)
that's work for me.