class Meta: model = models.Project fields = ['name', 'color', 'desc'] widgets = { 'desc': forms.Textarea, 'color': ColorRadioSelect(attrs={"class": "color-radio"}), # 自定义组件 }
class Meta: model = models.Project fields = ['name', 'color', 'desc'] widgets = { 'desc': forms.Textarea, 'color': ColorRadioSelect(attrs={'class': 'color-radio'}) }
class Meta: model = models.Project fields = ['name', 'color', 'desc'] widgets = { 'desc': forms.Textarea, # 等同于desc = forms.CharField(widget=forms.Textarea(attrs={'xx': 123})) 'color': ColorRadioSelect(attrs={'class': 'color-radio'}) }
class Meta: model = models.ProjectInfo fields = ['name', 'color', 'desc'] widgets = { 'desc': forms.Textarea, 'color':ColorRadioSelect(attrs={'class':'color-radio'}), # 可以添加属性 attr={xx=xxx} }
class Meta: model = models.Project fields = ['name', 'color', 'desc'] widgets = { 'desc': forms.Textarea(), #''color':forms.RadioSelect #color字段包含choices参数,默认是select下拉框,这里改成RadioSelect #下面使用的是自定义的插件,继承RadioSelect,并把其中生成的ul li标签替换掉,那么前端展示的时候会横着显示,并且还可以自己加span标签 'color': ColorRadioSelect(attrs={'class': 'color-radio'}) }
class Meta: model = models.Project fields = ["name", "color", "desc"] # 方式2 重写models中字段且通过attrs参数为该字段新增属性 widgets = { # "desc": forms.Textarea(attrs={"xx": 123}) "desc": forms.Textarea, # "color": forms.Select # 添加项目时颜色的选择方式:默认Select框显示,修改为radio显示 "color": ColorRadioSelect(attrs={"class": "color-radio"}) # 自定义的插件 }
class Meta: model = Project fields = ['name', 'color', 'desc'] widgets = { 'desc': forms.Textarea, # 默认是Select类型,修改成RadioSelect # 'color': forms.Select # 'color': forms.RadioSelect, 'color': ColorRadioSelect(attrs={'class': 'color-radio'}), }