[Answered ]-Django admin inline instances only if they exist

2👍

You should override ModelAdmin.get_formsets_with_inlines.

class MyModelAdmin(admin.ModelAdmin):
    inlines = [MyInline, SomeOtherInline]

    def get_formsets_with_inlines(self, request, obj=None):
        for inline in self.get_inline_instances(request, obj):
            # FILTER THE INLINE FORMSET TO YIELD HERE 
            # For example, given obj.related_instances value
            if obj is not None and obj.related_instances.count() > 0:
                yield inline.get_formset(request, obj), inline

See django doc here.

Leave a comment