1

I would like to specify a css class to the fieldset (or parent divs) for the inlines specified in a ModelAdmin class, similar to what is specified for fieldsets. For example:

class Set2Inline(admin.TabularInline):
    ...

class MyModelAdmin(admin.ModelAdmin):
    fieldsets = [
                ('Set1', {'fields': ['number', 'name', 'etc'], 'classes': ['toggle']})
                ]
    inlines = [Set2Inline]

Is there a way to add a class in similar fashion to 'inlines', such as:

inlines = [
              'Set2', {Set2Inline, 'classes': ['toggle']}
          ]

It doesn't matter if the class is attached to either the fieldset of the inline or its parent divs. What is important is to be able to specify a unique class for each inline in similar fashion to what is done for each fieldset. Is there a way to do this? If it can be done in the Set2Inline class that would be fine as well.

1
  • I think you'd have to define your own template based on the built in django/contrib/admin/templates/admin/edit_inline/tabular.html, then either set the template attribute on your Set2Inline or define a new subclass of InlineModelAdmin - I don't see anything in the standard templates that would allow this. Commented Nov 5, 2013 at 0:26

2 Answers 2

2

Like Peter's comment suggests, pretty much the only way you can achieve this is by creating a custom template for Set2Inline based on django/contrib/admin/templates/admin/edit_inline/tabular.html:

# admin.py
class Set2Inline(admin.TabularInline):
    template = 'myapp/templates/myapp/admin/tabular.html'

{# myapp/templates/myapp/admin/tabular.html #}
{% load i18n admin_static admin_modify %}
<div class="inline-group your-class" id="{{ inline_admin_formset.formset.prefix }}-group">
  <div class="tabular inline-related {% if forloop.last %}last-related{% endif %}">
{{ inline_admin_formset.formset.management_form }}
<fieldset class="module your-fieldset-class">
...

An alternative could be adding a javascript file to Media in Set2Inline that adds the classes using jQuery:

class Set2Inline(admin.TabularInline):
    class Media:
        js = 'myapp/admin/addClasses.js',

// myapp/static/myapp/admin/addClasses.js
(function($) {
    $('#mymodel-group').addClass('your-class');
    $('#mymodel-group fieldset.module').addClass('your-fieldset-class');
})(django.jQuery);
Sign up to request clarification or add additional context in comments.

1 Comment

Upvoted for expanding my relatively short comment. I'd hope that the jQuery version wouldn't be necessary, since if you can write the correct selectors in jQuery you can probably also write them in your CSS rather than relying on a class name, but that does depend on the exact situation.
2

Yes you can,

class Set2Inline(admin.TabularInline):  # extending any admin Inline Class will work AFAIK
    ...
    classes = ['collapse',]
    ...

Working in Django 2.1, not sure added in which version.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.