Search code examples
djangodjango-admin

How to show __str__ representations for existing Inline objects in the admin UI, but still use the form for adding new objects


I have a Django Admin view with a number of inlines. The 'Add another ' works as expected and brings up an empty form to fill in the values.

Once added though, it shows that related object in form view, and all fields on all inlines remain editable.

What I'd like to achieve is that all existing objects are listed in their __str__ representation, but the 'Add another' button still brings up the form.

Can this be done in Django Admin, or am I looking for too much?


Solution

  • Django does indeed support this. This functionality is provided by the ModelAdmin.has_change_permission method. You would use it like this:

    class FoobarModelInline(admin.TabularInline):
        model = Foobar
    
        def has_change_permission(self, request, obj=None):
            return False
    

    You might also be interested in the has_delete_permission method.