You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.9 KiB
45 lines
1.9 KiB
2 years ago
|
from django import forms
|
||
|
from django.core.exceptions import ValidationError
|
||
|
|
||
|
from apps.goods.models import *
|
||
|
from apps.goods.views import *
|
||
|
|
||
|
|
||
|
class GoodsCateForm(forms.Form):
|
||
|
name = forms.CharField(label='分类名称', min_length=2, widget=forms.widgets.TextInput(
|
||
|
attrs={'class': 'form-control', 'placeholder': '请输入分类名称'}),
|
||
|
error_messages={'required': '分类名称不能为空!', 'min_length': '长度最小2位'})
|
||
|
parent_id = forms.CharField(label='选择父类', max_length=20, required=True,
|
||
|
widget=forms.widgets.Select(
|
||
|
attrs={'class': 'form-control custom-select', 'placeholder': '选择父类'}),
|
||
|
error_messages={'required': '请选择父类'})
|
||
|
sort = forms.CharField(label='排序', widget=forms.widgets.TextInput(
|
||
|
attrs={'class': 'form-control', 'placeholder': '请输入数字'}), error_messages={'required': '排序不能为空'})
|
||
|
logo = forms.ImageField(label='分类图片', required=False,
|
||
|
widget=forms.widgets.FileInput(attrs={'class': 'custom-file-input'}))
|
||
|
|
||
|
def __init__(self, *args, **kwargs):
|
||
|
super(GoodsCateForm, self).__init__(*args, **kwargs)
|
||
|
cate_all = GoodsCategory.objects.all()
|
||
|
self.alist = [('', '请选择...')]
|
||
|
self.fields['parent_id'].widget.choices = self.bind_data(cate_all, 0, 1)
|
||
|
|
||
|
def bind_data(self, datas, _id, n):
|
||
|
if _id == 0:
|
||
|
datas = datas.filter(parent__isnull=True)
|
||
|
else:
|
||
|
datas = datas.filter(parent_id=id)
|
||
|
for data in datas:
|
||
|
self.alist.append((data.id, self.space_length(n) + data.name))
|
||
|
|
||
|
self.bind_data(datas, data.id, n+2)
|
||
|
print(self.alist)
|
||
|
return self.alist
|
||
|
|
||
|
@staticmethod
|
||
|
def space_length(i):
|
||
|
space = ''
|
||
|
for j in range(1, i):
|
||
|
space += '--'
|
||
|
return space + '|--'
|