Python Django ModelMultipleChiceField对象对\u field\u name没有属性
我正在尝试为ModelForm创建一个自定义字段。我从ModelMultipleEchoIceField扩展,然后覆盖render和render_选项,但是,我在尝试导入表单时一直遇到此异常:Python Django ModelMultipleChiceField对象对\u field\u name没有属性,python,django,model,modelform,Python,Django,Model,Modelform,我正在尝试为ModelForm创建一个自定义字段。我从ModelMultipleEchoIceField扩展,然后覆盖render和render_选项,但是,我在尝试导入表单时一直遇到此异常: AttributeError:'modelmultipechoicefield'对象没有属性'to\u field\u name' 我不确定我错过了什么。我甚至尝试过在我的新类中添加一个to_field_name属性,但这没有帮助。这是我的密码: class MultiSelect(ModelMultip
AttributeError:'modelmultipechoicefield'对象没有属性'to\u field\u name'
我不确定我错过了什么。我甚至尝试过在我的新类中添加一个to_field_name属性,但这没有帮助。这是我的密码:
class MultiSelect(ModelMultipleChoiceField):
def __init__(self, queryset, cache_choices=False, required=True,
widget=None, label=None, initial=None, help_text=None, *args, **kwargs):
super(MultiSelect, self).__init__(queryset, cache_choices, required, widget,
label, initial, help_text, *args, **kwargs)
def render_options(self, name, choices, selected_choices):
output = []
i = 0
for option_value, option_label in chain(self.choices, choices):
checked_html = (option_value in selected_choices) and u' checked="checked"' or ''
class_html = (i % 2 == 0) and u'even' or u'odd'
output.append('<li class="{0}"><input type="checkbox" name="{1}" value="{2}"{3}/>{4}</li>'
.format(class_html, name, escape(option_value), checked_html, escape(option_label)))
i += 1
def render(self, name, value, attrs=None, choices=()):
if value is None: value = []
final_attrs = self.build_attrs(attrs, name=name)
output = [u'<ul class="multiSelect">']
options = self.render_options(name, choices, value)
if options:
output.append(options)
output.append('</ul>')
return mark_safe(u'\n'.join(output))
class RoleForm(ModelForm):
class Meta:
model = Role
exclude = ('user_id',)
widgets = {
'permissions': MultiSelect(queryset=Permission.objects.all())
}
class MultiSelect(modelmultipechoicefield):
定义初始化(self,queryset,cache\u choices=False,required=True,
widget=None、label=None、initial=None、help_text=None、*args、**kwargs):
超级(多选,自)。\uuuu初始化(查询集,缓存选项,必选,小部件,
标签、首字母、帮助文字、*args、**kwargs)
def渲染_选项(自身、名称、选项、选定_选项):
输出=[]
i=0
对于选项值,选项标签位于链中(self.choices,choices):
选中的\u html=(所选选项中的选项\u值)和u'checked=“checked”或“”
类_html=(i%2==0)和u'偶数'或u'奇数'
append('{4} '
.format(类\ html、名称、转义(选项\值)、选中\ html、转义(选项\标签)))
i+=1
def render(self、name、value、attrs=None、choices=()):
如果值为None:value=[]
final\u attrs=self.build\u attrs(attrs,name=name)
输出=[u'']
选项=self.render_选项(名称、选项、值)
如果选择:
output.append(选项)
output.append(“
”)
返回标记\u safe(u'\n'.连接(输出))
类角色表单(模型表单):
类元:
模型=角色
排除=('user_id',)
小部件={
“权限”:多选(queryset=Permission.objects.all())
}
每当我从myapp.forms import RoleForm简单地执行一个,我就会得到上面的错误
我是否应该向我的类中添加我缺少的内容?根据您发布的代码,我不确定为什么会出现问题,但是to_field\u name
是modelcooicefield
上的一个属性:
class ModelChoiceField(ChoiceField):
...
def __init__(self, queryset, empty_label=u"---------", cache_choices=False,
required=True, widget=None, label=None, initial=None,
help_text=None, to_field_name=None, *args, **kwargs):
...
self.to_field_name = to_field_name
但是,当modelmultipechoicefield
子类modelcooicefield
时,它的\uuuuuu init\uuuuuuu
方法不接受to\u field\u name
作为关键字参数。它显然依赖于modelcooicefield
的默认行为,即设置self.to\u field\u name
的默认值None
您的子类也应该这样做,所以这部分比较混乱。您似乎混淆了字段和小部件。您继承自modelmultipechoicefield
,它(顾名思义)是一个字段,而不是小部件。但是render
和render\u选项
是小部件上的方法,而不是字段。您已经在widgets
字典中使用了您的类
我猜你是想创建一个小部件。您应该继承一个小部件类,可能是表单。复选框selectmultiple
是的,您是对的。我是。。。而CheckboxSelectMultiple正是我一直在寻找的!我怎么会错过这个?!谢谢现在,我想知道如何删除这条恼人的帮助信息。