我已经构建了另一个相当通用的解决方案...
在你的admin.py中为Inline添加一个新字段:
class YourModelInline(admin.TabularInline):
model = YourModel
after_field = "fieldname_of_field_before_inline"
然后,定制包含内联的模型的AdminClass的render_change_form:
class EditModelAdmin(model.ModelAdmin):
inlines = [YourModelInline,]
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
sorted_inline_formsets = {}
inline_admin_formsets = context['inline_admin_formsets']
formsets_to_remove = []
for inline_formset in inline_admin_formsets:
if hasattr(inline_formset.opts, 'after_field'):
fieldname = inline_formset.opts.after_field
if fieldname in sorted_inline_formsets:
sorted_inline_formsets[fieldname].append(inline_formset)
else:
sorted_inline_formsets.update({
fieldname: [inline_formset,]
})
formsets_to_remove.append(inline_formset)
for inline_formset in formsets_to_remove:
inline_admin_formsets.remove(inline_formset)
context.update({
'sorted_inline_formsets': sorted_inline_formsets,
'inline_admin_formsets': inline_admin_formsets
})
return super(EditModelAdmin, self).render_change_form(request, context, add=add,
change=change, obj=obj, form_url=form_url)
我们将所有带有额外字段的内联元素移动到自己的字典中,其中字段名作为键...
为了正确呈现,请创建文件/templates/admin/includes/fieldset.html,覆盖标准的Django fieldset.html,并使用以下内容:
{% load custom_filter %}
<fieldset class="module aligned {{ fieldset.classes }}">
{% if fieldset.name %}<h2>{{ fieldset.name }}</h2>{% endif %}
{% if fieldset.description %}
<div class="description">{{ fieldset.description|safe }}</div>
{% endif %}
{% for line in fieldset %}
<div class="form-row{% if line.fields|length_is:'1' and line.errors %} errors{% endif %}{% if not line.has_visible_field %} hidden{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}">
{% if line.fields|length_is:'1' %}{{ line.errors }}{% endif %}
{% for field in line %}
<div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}"{% elif field.is_checkbox %} class="checkbox-row"{% endif %}>
{% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
{% if field.is_checkbox %}
{{ field.field }}{{ field.label_tag }}
{% else %}
{{ field.label_tag }}
{% if field.is_readonly %}
<div class="readonly">{{ field.contents }}</div>
{% else %}
{{ field.field }}
{% endif %}
{% endif %}
{% if field.field.help_text %}
<div class="help">{{ field.field.help_text|safe }}</div>
{% endif %}
</div>
{% if field.field.name %}
{% with field.field.name as fieldname %}
{% if sorted_inline_formsets|get_dict_value:fieldname != False %}
{% for inline_admin_formset in sorted_inline_formsets|get_dict_value:fieldname %}
{% include inline_admin_formset.opts.template %}
{% endfor %}
{% endif %}
{% endwith %}
{% endif %}
{% endfor %}
</div>
{% endfor %}
</fieldset>
这将会在相应字段后添加排序行内元素... 现在您只需要为在Django模板中使用字典编写custom_filter,创建templatetags/custom_filter.py并添加:
@register.filter
def get_dict_value(dict, key):
if key in dict:
return dict[key]
else:
return False
看这里:您可以将任何字段名称输入到任何内联中,以在该字段之后添加它... 这需要一些工作来设置,但如果您有几个内联需要排序,这可能是更清晰的方法...