[Answered ]-Django Admin – Prevent Inline Showing When Adding a New Object

1👍

Add extra=0 in your ProfileInline to prevent create new profile instance when you create new User at admin side…

    class ProfileInline(admin.TabularInline):
        model = Profile
        can_delete = False
        extra = 0
    class UserAdmin(admin.ModelAdmin):
        list_display = ('name',)
        inlines = [ProfileInline,]

OR You can use ModelAdmin get_inline_instances function. Following code removes inlines from add_view:

    class ProfileInline(admin.TabularInline):
        model = Profile
        can_delete = False
    
    class UserAdmin(admin.ModelAdmin):
        list_display = ('name',)
        inlines = [ProfileInline,]
    
        def get_inline_instances(self, request, obj=None):
            return obj and super(UserAdmin, self).get_inline_instances(request, obj) or []

Leave a comment