class DepartForm forms ModelForm class Meta model Depart def build_cho

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class DepartForm(forms.ModelForm):
class Meta:
model = Depart
def build_choices():
arr = [('0','Верхний уровень')]
for object in Depart.objects.all().order_by('tree_id','level'):
arr.append(tuple(["%s" % object.id, "- " * object.level + "%s" % object.name]))
return tuple(arr)
parent = forms.ChoiceField(label=u'Родительское подразделение', choices=build_choices(), required=True)
def clean_parent(self):
value = self.cleaned_data['parent']
if int(value):
try:
return Depart.objects.get(pk=value)
except Depart.DoesNotExist:
raise forms.ValidationError('Depart %s does not exist' % value)