У меня была похожая проблема, но я использовал немного другую разметку. Вам необходимо создать собственный виджет для отображения категорий.
Вот как я сделал это мое:
from itertools import chain
from django import forms
from django.db import models
from django.forms.widgets import CheckboxSelectMultiple
from django.utils.encoding import force_unicode
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe
class CustomCheckboxSelectMultiple(forms.CheckboxSelectMultiple):
items_per_row = 1 # Number of items per row
def render(self, name, value, attrs=None, choices=()):
if value is None: value = []
has_id = attrs and 'id' in attrs
final_attrs = self.build_attrs(attrs, name=name)
output = ['<table><tr>']
# Normalize to strings
str_values = set([force_unicode(v) for v in value])
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
# If an ID attribute was given, add a numeric index as a suffix,
# so that the checkboxes don't all have the same ID attribute.
if has_id:
final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
label_for = ' for="%s"' % final_attrs['id']
else:
label_for = ''
cb = forms.CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
option_value = force_unicode(option_value)
rendered_cb = cb.render(name, option_value)
option_label = conditional_escape(force_unicode(option_label))
if i != 0 and i % self.items_per_row == 0:
output.append('</tr><tr>')
#Here you need to put you layout logic for display the checkboxes
if self.choices[i][1].parent_id is None:
output.append('<td nowrap><label%s>%s</label></td>' % (label_for, option_label))
else:
output.append('<td nowrap><label%s>%s %s</label></td>' % (label_for, rendered_cb, option_label))
output.append('</tr></table>')
return mark_safe('\n'.join(output))
В models.py у меня есть:
class MyModelForm(ModelForm):
my_model_types = forms.MultipleChoiceField(label='My model types', widget = CustomCheckboxSelectMultiple, required=True)
class Meta:
model = MyModel
def __init__(self, *args, **kwargs):
super(self.__class__, self).__init__(*args, **kwargs)
tree = Tree()
tree.build_tree(reasons=MyModelType.objects.all())
list = tree.as_list()
CHOICES = [(rt.pk, rt) for rt in list]
self.fields['reason_types'].choices = CHOICES