Django MultiValueField - 如何将选择传递给ChoiceField? [英] Django MultiValueField - How to pass choices to ChoiceField?
问题描述
__ init __()
得到一个意想不到的关键字参数'选择'。 我知道其余代码的工作原理是因为当我从 __ init __
和super中删除choices关键字参数时,多值域正确显示,但没有任何选择。
这是我如何设置自定义多值域:
code> class InputAndChoice(object):
def __init __(self,text_val ='',choice_val =''):
self.text_val = text_val
self.choice_val = choice_val
class InputAndChoiceWidget(widgets.MultiWidget):
def __init __(self,attrs = None):
widget =(widgets.TextInput(),
widgets.Select ()
)
super(InputAndChoiceWidget,self).__ init __(widget,attrs = attrs)
def解压缩(self,value):
如果value:
return [value.text_val,value.choice_val]
返回[无,无]
class InputAndChoiceField(forms.MultiValueField):
widget = In putAndChoiceWidget
def __init __(self,required = True,widget = None,label = None,initial = None,
help_text = None,choices = None):
field =
fields.CharField(),
fields.ChoiceField(choices = choices),
)
super(InputAndChoiceField,self).__ init __(fields = field,widget = widget,
label = label,initial = initial,help_text = help_text,choices = choices)
而且这样调用:
input_and_choice = InputAndChoiceField(choices = [(1,'first')),(2,'second' )])
那么如何将选择传递给我的ChoiceField字段?
编辑:
我已经尝试过stefanw的建议,但还是没有运气。我使用logging.debug来打印出init和self.fields [1]结尾处的InputAndChoiceField的内容,其中包含正确的值,但是它不会在浏览器中显示任何选项。
我遇到这个完全相同的问题,解决方案如下:
class InputAndChoiceWidget(widgets.MultiWidget):
def __init __(self,* args,** kwargs):
myChoices = kwargs.pop(choices)
widgets =(
widgets.TextInput(),
widgets.Select(choices = myChoices)
)
super(InputAndChoiceWidget,self).__ init __(widgets,* args,* * kwargs
class InputAndChoiceField(forms.MultiValueField):
widget = InputAndChoiceWidget
def __init __(self,* args,** kwargs):
#你也可以使用一些fn来返回选项;
#的重点是,他们动态设置
myChoices = kwargs.pop(choices,[(default,default choice)])
fields =(
fields.CharField(),
fields.ChoiceField(choices = myChoices),
)
super(InputAndChoiceField,self).__ init __(fields,* args,** kwargs)
#这里是选择设置的地方:
self.widget = InputAndChoiceWidget(choices = myChoices)
向窗口小部件的构造函数添加选择kwarg。然后在创建该字段之后显式调用构造函数。
I have a multivaluefield with a charfield and choicefield. I need to pass choices to the choicefield constructor, however when I try to pass it into my custom multivaluefield I get an error __init__()
got an unexpected keyword argument 'choices'.
I know the rest of the code works because when I remove the choices keyword argument from __init__
and super, the multivaluefield displays correctly but without any choices.
This is how I setup my custom multivaluefield:
class InputAndChoice(object):
def __init__(self, text_val='', choice_val=''):
self.text_val=text_val
self.choice_val=choice_val
class InputAndChoiceWidget(widgets.MultiWidget):
def __init__(self, attrs=None):
widget = (widgets.TextInput(),
widgets.Select()
)
super(InputAndChoiceWidget, self).__init__(widget, attrs=attrs)
def decompress(self,value):
if value:
return [value.text_val, value.choice_val]
return [None, None]
class InputAndChoiceField(forms.MultiValueField):
widget = InputAndChoiceWidget
def __init__(self, required=True, widget=None, label=None, initial=None,
help_text=None, choices=None):
field = (
fields.CharField(),
fields.ChoiceField(choices=choices),
)
super(InputAndChoiceField, self).__init__(fields=field, widget=widget,
label=label, initial=initial, help_text=help_text, choices=choices)
And I call it like so:
input_and_choice = InputAndChoiceField(choices=[(1,'first'),(2,'second')])
So how do I pass the choices to my ChoiceField field?
Edit:
I've tried stefanw's suggestion but still no luck. I've used logging.debug to print out the contents of InputAndChoiceField at the end of the init and self.fields[1].choices contains the correct values as per above however it doesnt display any choices in the browser.
I ran into this exact same problem and solved it like this:
class InputAndChoiceWidget(widgets.MultiWidget):
def __init__(self,*args,**kwargs):
myChoices = kwargs.pop("choices")
widgets = (
widgets.TextInput(),
widgets.Select(choices=myChoices)
)
super(InputAndChoiceWidget, self).__init__(widgets,*args,**kwargs)
class InputAndChoiceField(forms.MultiValueField):
widget = InputAndChoiceWidget
def __init__(self,*args,**kwargs):
# you could also use some fn to return the choices;
# the point is, they get set dynamically
myChoices = kwargs.pop("choices",[("default","default choice")])
fields = (
fields.CharField(),
fields.ChoiceField(choices=myChoices),
)
super(InputAndChoiceField,self).__init__(fields,*args,**kwargs)
# here's where the choices get set:
self.widget = InputAndChoiceWidget(choices=myChoices)
Add a "choices" kwarg to the widget's constructor. Then explicitly call the constructor after the field is created.
这篇关于Django MultiValueField - 如何将选择传递给ChoiceField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!