我正在嘗試爲ModelForm創建一個自定義字段。我是從ModelMultipleChoiceField然而延伸,然後重寫渲染和render_options,我不斷收到此異常時,只是試圖導入我的表格:Django ModelMultipleChoiceField對象沒有屬性to_field_name
AttributeError: 'ModelMultipleChoiceField' object has no attribute 'to_field_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())
}
每當我簡單地做一個from myapp.forms import RoleForm
,我會得到上面的錯誤。
我應該向我的班級添加一些我錯過的東西嗎?
是的,你說得對。我是...和CheckboxSelectMultiple是我一直在尋找的東西!我怎麼想那個?謝謝。現在要弄清楚如何刪除惱人的help_text消息。 – intargc