分别显示每个内联

时间:2015-12-23 09:57:51

标签: django django-admin inline-formset

我为我的某个应用做了自定义change_form。

class InboundTourDatesInline(admin.StackedInline):
    model = InboundTourDates
    extra = 1


class InboundTourPhotosInline(admin.StackedInline):
    model = InboundTourPhotos
    extra = 1


class InboundAdmin(admin.ModelAdmin, ListView):
    model = Inbound
    change_form_template = 'admin/tour/inbound_form.html'
    form = InboundForm
    filter_horizontal = ('program', 'tabs', 'icon', 'season')
    list_filter = ('label', 'start_date')
    list_display = ['title']
    inlines = [InboundTourDatesInline, InboundTourPhotosInline]

在模板中,我在不同的标签中显示一些表单字段。另外,我想在不同的标签中显示每个内联。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我只使用两个for循环来构建nav-pills和tabs。对于导航,我使用唯一的形式前缀。

<ul class="nav nav-pills nav-stacked tabs">
    {% for inline_admin_formset in inline_admin_formsets %}
        <li role="presentation">
            <a role="tab" data-toggle="tab" aria-expanded="false" href="#{{ inline_admin_formset.formset.prefix }}">{{ inline_admin_formset.opts.verbose_name_plural }}</a>
        </li>
    {% endfor %}
</ul>

<div class="tab-content">
    <div id="main" class="tab-pane" role="tabpanel">
        {% for inline_admin_formset in inline_admin_formsets %}
            {{ inline_admin_formset.formset.management_form }}
            {{ inline_admin_formset.formset.non_form_errors }}
            <div id="{{ inline_admin_formset.formset.prefix }}" class="tab-pane" role="tabpanel">
                <h2>{{ inline_admin_formset.opts.verbose_name_plural }}</h2>
                {% include inline_admin_formset.opts.template %}
            </div>
        {% endfor %}
    </div>
</div>