5

When I create a TabularInline in my Django's admin panel, it displays a title per record. How can I change this title? How can I delete this title?

I include a screenshot below. The title I am referring to is here ExportLookupTableElement object. The lines without that title are the extra fields to add new ones. I want the entire table to look like this.

Screenshot

physicalattraction
  • 6,485
  • 10
  • 63
  • 122

3 Answers3

12

You can remove this title by overriding Django's admin css:

  1. Create css/custom_admin.css in your static directory with following code:
.inline-group .tabular tr.has_original td {
    padding-top: 8px;
}

.original {
    display: none;
}
  1. Edit your admin.py file to include this extra css for ModelAdmin that you want to remove title:
class TestDetailInline(admin.TabularInline):
    model = TestDetail

class TestAdmin(admin.ModelAdmin):
    class Media:
        css = {
            'all': ('css/custom_admin.css', )     # Include extra css
        }
    inlines = [TestDetailInline]

example django admin after override

Or you can override css for all admin pages by following this answer.

Community
  • 1
  • 1
nattster
  • 824
  • 2
  • 11
  • 18
1

Based on nattster's answer I did this:

edit/create templates/admin/base_site.html:

    {% extends "admin/base.html" %}
    {% block extrastyle %}
        <link rel="stylesheet" href="{% static "admin/css/admin_overrides.css" %}" />
    {% endblock %}

create admin/css/admin_overrides.css:

    /* StackedInline */
    .inline-group .hide-title h3 b, .inline-group .hide-title h3 .inline_label {
        visibility: hidden;
    }

    /* TabularInline */
    .inline-group .tabular .hide-title .original > p {
        display: none;
    }

    .inline-group .tabular .hide-title tr.has_original td {
        padding-top: 8px;
    }

Now you can hide the title for some inlines while keep it for others:

    class TestTabularInline(admin.TabularInline):
        classes = ("hide-title",)
        ...
    
    class TestStackedInline(admin.StackedInline):
        classes = ("hide-title",)
        ...

x0nix
  • 360
  • 1
  • 5
-1

If you can afford it for the purposes of your inlined model, returning an empty string from its __str__ method has exactly the same effect as solutions provided above. The title element is still in the HTML, but no longer takes up any visible space.

    class TestDetail(models.Model):
        ...
        def __str__(self):
            return ''