1

i have a question on how do i get rid of the ---- from the select drop down menu not to show the null(---) in the first row. I found from stackoverflow on RadioSelect and i manage to get rid of the --- but i am stuck in the select drop down menu...:( here is my coding example.

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)

forms.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
                   }

I did like the COLORSELECT to show as dropdown menu and also not showing the ---- in the first row. But if im using like the above code i get COLORSELECT as RadioSelect and NOT showing ---- (which is what i am want for not showing ---) but not as RadioSelect.

Thank you so much in advance.

okm
  • 23,575
  • 5
  • 83
  • 90
noobes
  • 161
  • 2
  • 18
  • 1
    What about this: http://stackoverflow.com/questions/8220404/django-how-to-exclude-the-bogus-option-from-select-element-generated-fro – dani herrera Oct 12 '12 at 00:06
  • Have you tried removing blank=True from the model definition? – qdot Oct 12 '12 at 00:24
  • Hi thank you for quick reply. I did try but i get an error "'module' object has no attribute 'TypedChoiceField'". what is that mean? – noobes Oct 12 '12 at 00:25
  • Hi qdot. yes i did but no luck..i did also make it blank=False and default=0 and default=1 but no luck..:( – noobes Oct 12 '12 at 00:31

1 Answers1

1

models.CharField uses TypedChoiceField form field when there are choices, by default. Thus no need to manually specify it (also its django.forms.fields.TypedChoiceField).

Try to remove blank=True and set a default value(or providing initial value in formfield, normally you don't have to do this, ModelForm would process it for you). Also its not meaningful to have CharField nullable; and I'm switching variable names COLORSELECT and colorselect by convention.

>>> COLORSELECT = (("1" , 'Yellow'),
...               ("2" , 'Red'),
...               ("3" , 'Blue'),
...               ("4" , 'Black'),)

>>> colorselect = models.CharField(max_length=2, choices=COLORSELECT, default='1')
>>> colorselect.formfield().__class__
django.forms.fields.TypedChoiceField

>>> print(colorselect.formfield().widget.render('','1'))
<select name="">
<option value="1" selected="selected">Yellow</option>
<option value="2">Red</option>
<option value="3">Blue</option>
<option value="4">Black</option>
</select>
okm
  • 23,575
  • 5
  • 83
  • 90