在您的代码中,调用超类初始化器后,设置选项如下:
class TooltipSelectWidget(Select) :
def __init__(self, *args, **kwargs) :
super().__init__(*args, **kwargs)
self.choices = [] # List of 2-tuple values
如果您查看Select 类(自定义小部件的基类)的__init__ 的代码,您可以直观地了解为什么这是解决方案:
class Select(Widget):
allow_multiple_selected = False
def __init__(self, attrs=None, choices=()):
super(Select, self).__init__(attrs)
# choices can be any iterable, but we may need to render this widget
# multiple times. Thus, collapse it into a list so it can be consumed
# more than once.
self.choices = list(choices)
更好的方法是在您的
中默认包含您的自定义选项
class TooltipSelectWidget(Select) :
CUSTOM_CHOICES = () # Modify at will.
def __init__(self, attrs=None, choices=CUSTOM_CHOICES) :
super().__init__(attrs=attrs, choices=choices)