最初の行に null(---) を表示しないように、選択ドロップダウン メニューから ---- を削除する方法について質問があります。RadioSelect の stackoverflow から見つけて、なんとか --- を取り除くことができましたが、選択ドロップダウンメニューで立ち往生しています...:( これが私のコーディング例です。
models.py
colorradio = (("1" , 'Yellow'),
("2" , 'Red'),
("3" , 'Blue'),
("4" , 'Black'),)
COLORRADIO = models.CharField(max_length = 2, choices = colorradio, null = True, blank = True)
colorselect= (("1" , 'Yellow'),
("2" , 'Red'),
("3" , 'Blue'),
("4" , 'Black'),)
COLORSELECT= models.CharField(max_length = 2, choices = colorselect, null = True, blank = True)
フォーム.py
class RadioSelectNotNull(RadioSelect, Select):
def get_renderer(self, name, value, attrs=None, choices=()):
"""Returns an instance of the renderer."""
if value is None: value = ''
str_value = force_unicode(value) # Normalize to string.
final_attrs = self.build_attrs(attrs)
choices = list(chain(self.choices, choices))
if choices[0][0] == '':
choices.pop(0)
return self.renderer(name, str_value, final_attrs, choices)
class RainbowForm(ModelForm):
class Meta:
model = Rainbow
widgets = {'COLORRADIO':RadioSelectNotNull(), # this is correct and NOT shown ---
'COLORSELECT':RadioSelectNotNull(), #should be in dropdown menu
}
COLORSELECT
をドロップダウン メニューとして表示し、最初の行に ---- を表示しないようにしました。しかし、上記のコードのように使用すると、COLORSELECT
asRadioSelect
と NOT が表示されます ---- (これは、表示されないようにしたいものです ---) しかし、 as ではありませんRadioSelect
。
よろしくお願いします。