templatetags/fieldsetform.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from django.template.loader import render_to_string
from django import template
register = template.Library()
def make_from(form, fieldsets):
for title, options in fieldsets:
fields = [form[field] for field in options['fields'] if field not in form.Meta.exclude]
if not fields:
continue
yield (title, fields, options)
@register.filter
def fieldsets(form):
try:
return render_to_string('parts/fieldset_form.html',
{'fieldsets': make_from(form, form.Meta.model._meta.admin.fields)})
except AttributeError:
return form