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.
49 lines
2.1 KiB
49 lines
2.1 KiB
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=False, |
|
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_all, _id, n): |
|
if _id == 0: |
|
print(111111111111) |
|
datas = datas_all.filter(parent__isnull=True) |
|
print(datas) |
|
else: |
|
print(222222222222) |
|
datas = datas_all.filter(parent_id=_id) |
|
print(datas) |
|
for data in datas: |
|
print(data) |
|
self.alist.append((data.id, self.space_length(n) + data.name)) |
|
|
|
self.bind_data(datas_all, 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 + '|--'
|
|
|