我有一个模型来跟踪一段时间内个人的行为。该模型用作我所谓的“外壳模型”的内联。我的inilne管理员模型如下:
class IndicatorInline(admin.StackedInline):
formfield_overrides = {
models.CharField: {'widget': TextInput(attrs={'size':'20'})},
}
model = Indicator
insert_after = 'person'
can_delete = False
template = "admin/trajectory/indicator/edit_inline/stacked.html"
readonly_fields = ["name",]
fieldsets = (
("Name", {"fields": ("name",)}),
("Primary", {"fields": (("primary_date", "primary_date_cue"),
"primary_date_confidence")}),
("Secondary", {"fields": (("secondary_date", "secondary_date_cue"),
"secondary_date_confidence")}),
("Tertiary", {"fields": (("tertiary_date", "tertiary_date_cue"),
"tertiary_date_confidence")}),
)
extra = 0
max_num = 26
def has_add_permission(self, request):
return False
这里extra = 0
和max_num=26
,因为只有26种已定义的行为,并且应用程序不在乎是否缺少某些行为,因此所有26种行为都同时添加。它们都是同一张表的一部分,因为它将使更常见的查询更加直观。
此外,当我添加这些“外壳”模型之一时,在调用shell.save()
时添加了26种行为,但是Django管理员仍在呈现额外的内联,我绝对不希望这样。
作为参考,我将包括我用于内联的自定义模板(必须编写自定义模板,以便内联能够呈现出美观的表)。
这里是stacked.html:
{% load i18n admin_urls static %}
<div class="js-inline-admin-formset inline-group"
id="{{ inline_admin_formset.formset.prefix }}-group"
data-inline-type="stacked"
data-inline-formset="{{ inline_admin_formset.inline_formset_data }}">
<fieldset class="module {{ inline_admin_formset.classes }}">
<h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
{{ inline_admin_formset.formset.management_form }}
{{ inline_admin_formset.formset.non_form_errors }}
{% for inline_admin_form in inline_admin_formset %}<div class="inline-related{% if inline_admin_form.original or inline_admin_form.show_url %} has_original{% endif %}{% if forloop.last and inline_admin_formset.has_add_permission %} empty-form last-related{% endif %}" id="{{ inline_admin_formset.formset.prefix }}-{% if not forloop.last %}{{ forloop.counter0 }}{% else %}empty{% endif %}">
{% if inline_admin_form.form.non_field_errors %}{{ inline_admin_form.form.non_field_errors }}{% endif %}
<table class="timeline-table" id="trajectoryprofile_form">
{% for fieldset in inline_admin_form %}
{% include "admin/includes/b_fieldset.html" %}
{% endfor %}
</table>
{% if inline_admin_form.needs_explicit_pk_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
{{ inline_admin_form.fk_field.field }}
</div>{% endfor %}
</fieldset>
</div>
这是我使用的自定义fieldset
模板:
<fieldset class="module aligned {{ fieldset.classes }}">
{% for line in fieldset %}
<td class="timeline-table" style="vertical-align:middle" width="13%">
{% for field in line %}
{{ field.field.label_tag }} <br/>
{% if field.is_readonly %}
<div class="readonly">{{ field.contents }}</div>
{% else %}
{{ field.field }}
{% endif %}
{% endfor %}
</td>
{% endfor %}
</fieldset>