Pedro
Pedro

Reputation: 85

Django tabular inline display

Is it possible to change the display of inlines in order to change it to this? I was thinking of changing the admin template file "tabular.html", is it going to be possible or should I change something else?

Update

Ok, I've been trying to edit the tabular.html but my experience with Django isn't enough to understand how/where to make the necessary changes... Any clue of where I should start? Shouldn't I be changing the CSS also? I guess that {{ field.field }} automatically renders the dropdown menu (Django admin default) if I'm understand this correctly...

Update 2

I was able to change the second column to the functionality that I wanted but I think that for the first one it's going to be trickier... Current status

Update 3

One hack that I think would work is to display on each of the inlines only one of the options of the first field and then deactivate the "add another option". How can I iterate on the options in "tabular.html" ?

Update 4

I guess the trick should be done here... How can I iterate on the field choices in order to display only one choice per line?

{% for fieldset in inline_admin_form %}
          {% for line in fieldset %}
            {% for field in line %}
            {{ field.get_choices_display }}
              <td class="{{ field.field.name }}">
              {% if field.is_readonly %}
                  <p>{{ field.contents }}</p>
              {% else %}
                  {{ field.field.errors.as_ul }}
                  {{ field.field }}
              {% endif %}
              </td>
            {% endfor %}
          {% endfor %}
        {% endfor %}

Upvotes: 2

Views: 5125

Answers (1)

okm
okm

Reputation: 23871

Yes, you could change template of your InlineModelAdmin instance to your customized template, for example customized_inline.html. Simply copy django/django/contrib/admin/templates/admin/edit_inline/tabular.html to customized_inline.html in your template path as starting.

edit

Perhaps I was misunderstanding. If you want to change the rendering style of a form field, the normal way is to change its widget. In Django ModelAdmin and InlineModelAdmin, the main ways of customizing a field widget goes around BaseModelAdmin.formfield_for_dbfield method inside django/contrib/admin/options.py, reading the code and the doc when you want to change the widget of a form field.

For field having choices, you could simply set radio_fields in ModelAdmin/InlineModelAdmin instance to render the field as radio select instead of dropdown.

Furthermore, use OneToOneField instead of ForeignKey, or set extra and max_num in your InlineModelAdmin instance to prevent admin from rendering multiple rows of inline, like:

class SomeInlineAdmin(admin.TabularInline):
    model = Foo
    extra = 1
    max_num = 1

I cannot open your second link, you could post things in the question instead of using a external link.

Upvotes: 4

Related Questions