Django ModelMultipleChoiceField object has no attribute to_field_name

对着背影说爱祢 提交于 2019-12-04 13:45:01

You seem to have got confused between fields and widgets. You inherit from ModelMultipleChoiceField, which (as the name implies) is a field, not a widget. But render and render_options are methods on widgets, not fields. And you've used your class in the widgets dictionary.

I suspect you do mean to create a widget. You should inherit from a widget class, probably forms.CheckboxSelectMultiple.

Not sure why it's a problem based on the code you've posted, but to_field_name is an attribute on ModelChoiceField:

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

However, when ModelMultipleChoiceField subclasses ModelChoiceField, it's __init__ method doesn't accept to_field_name as an keyword argument. It apparently relies on the ModelChoiceField's default behavior of setting self.to_field_name the default of None.

Your subclass should be doing the same, so that part is confusing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!