How to get rid of the bogus choice generated by RadioSelect of Django Form

后端 未结 5 983
名媛妹妹
名媛妹妹 2021-01-02 03:48

I am using ModelForm on Django 1.3.

models.py:

class UserProfile(models.Model):
...
gender = models.CharField(max_length=1, blank=True, choices=((\'M         


        
相关标签:
5条回答
  • 2021-01-02 04:24

    You can set the choices when you set the widget. It's showing the ---- because in your model you have blank=True.

    Just use the choices arg of the widget and set it to the choices you set in your model.

    0 讨论(0)
  • 2021-01-02 04:30

    Even without blank=True it shows the extra input. I have created a new Widget:

    from itertools import chain
    from django.forms import RadioSelect
    from django.utils.encoding import force_unicode
    
    class RadioSelectNotNull(RadioSelect):
        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)
    
    0 讨论(0)
  • 2021-01-02 04:32

    Set blank=False (or just remove it) and also add default='Unspecified'

    0 讨论(0)
  • 2021-01-02 04:37

    Django <= 1.10

    RadioSelectNotNull widget

    from itertools import chain
    from django.forms import RadioSelect
    from django.utils.encoding import force_unicode
    
    class RadioSelectNotNull(RadioSelect):
    """
    A widget which removes the default '-----' option from RadioSelect
    """
        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)
    

    Django >= 1.11

    As after Django 1.10 following method is no more in RadioSelect or in its ancestors. That's why upper widget will not remove bogus choice generated by RadioSelect.

    def get_renderer(self, name, value, attrs=None, choices=()):
    

    So to remove bogus choice generated by RadioSelect use following widget. I have tested this till Django 2.0

    from django.forms import RadioSelect
    class RadioSelectNotNull(RadioSelect):
        """
        A widget which removes the default '-----' option from RadioSelect
        """
        def optgroups(self, name, value, attrs=None):
            """Return a list of optgroups for this widget."""
            if self.choices[0][0] == '':
                self.choices.pop(0)
            return super(RadioSelectNotNull, self).optgroups(name, value, attrs)
    
    0 讨论(0)
  • 2021-01-02 04:42

    By default the widget used by ModelChoiceField will have an empty choice at the top of the list.

    You can change the text of this label (which is "---------" by default) with the empty_label attribute, or you can disable the empty label entirely by setting empty_label to None:

    A custom empty label:

    field1 = forms.ModelChoiceField(queryset=..., empty_label="(Nothing)")
    

    No empty label:

    field2 = forms.ModelChoiceField(queryset=..., empty_label=None)
    
    0 讨论(0)
提交回复
热议问题