根据文档:
所有 kwargs 字典都直接传递给表单字段的 __init__() 方法
def formfield(self, **kwargs):
# This is a fairly standard way to set up some defaults
# while letting the caller override them.
defaults = {'form_class': MyFormField}
defaults.update(kwargs)
return super(HandField, self).formfield(**defaults)
如果 MyFormField 是:
class GeopositionField(forms.MultiValueField):
def __init__(self, *args, **kwargs):
for name, value in kwargs.items(): # this doesn't print anything
print name
print value
self.widget = GeopositionWidget()
fields = (
forms.DecimalField(label=_('latitude')),
forms.DecimalField(label=_('longitude')),
)
super(GeopositionField, self).__init__(fields, required=False)
如何将参数传递给表单域并在init方法中获取?