class CategoryChoice forms ModelChoiceField def __init__ self args kwa

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class CategoryChoice(forms.ModelChoiceField):
def __init__(self, *args, **kwargs):
self.begin_from_level = kwargs.pop('begin_from_level', 0)
super(CategoryChoice, self).__init__(*args, **kwargs)
def label_from_instance(self, obj):
return obj.tree_represent(space='-', begin_from_level=self.begin_from_level)
class CategoryForm(forms.ModelForm):
parent = CategoryChoice(queryset=Category.objects.all(), required=False)
def __init__(self, *args, **kwargs):
super(CategoryForm, self).__init__(*args, **kwargs)
self.fields['parent'].queryset = Category.objects.filter(level__in=[0,1])
class Meta:
model = Category