如何使RadioSelect非强制性并删除ModelForm中的默认选项?

时间:2015-05-13 15:14:16

标签: django django-models django-forms

  

编辑我的问题与可能的副本不同的原因是因为这不能解决使其成为非   manditory 同时删除默认选项“-------”

我想在RadioSelect

中提出forms.ModelForm调查问题非强制性

使用RadioSelect我在我的ModelForm(下方)中添加了小部件,因为Djangos模型未提供RadioSelectSelect小部件。

执行此操作的标准方法是将Blank=True作为参数传递。但是,正如我在另一个问题中发现的那样,我问了it turns out that if you pass blank=True as an argument from models.CharField to the forms.RadioSelect widget it will leave the default option "-------" in place even if you use default=None.

我必须删除默认选项“-------”。

那么如何使RadioSelect问题不是强制性的,同时也不包括默认选项“-------”?

谢谢

forms.py

class SurveyFormB(forms.ModelForm): 

    class Meta:
        model = Person
        fields = ['internet_usage', 'smart_phone_ownership', 'smart_phone_usage']        

        widgets = {'internet_usage' : forms.RadioSelect,
                   'smart_phone_ownership' : forms.Select,
                   'smart_phone_usage' : forms.RadioSelect,
                   }

models.py

#How often do you use the Internet?  
INTERNET_LESS_THAN_ONE_HOUR_A_DAY = 'Less than one hour per day'
INTERNET_ONE_TO_TWO_HOURS_A_DAY = '1 - 2 hours per day'
INTERNET_TWO_TO_FOUR_HOURS_A_DAY = '2 - 4 hours per day'
INTERNET_FOUR_TO_SIX_HOURS_A_DAY = '4 - 6 hours per day'
INTERNET_SIX_TO_EIGHT_HOURS_A_DAY = '6 - 8 hours per day'
INTERNET_EIGHT_PLUS_HOURS_A_DAY = '8 + hours per day'

INTERNET_USAGE = (
    (INTERNET_LESS_THAN_ONE_HOUR_A_DAY, 'Less than one hour a day'),
    (INTERNET_ONE_TO_TWO_HOURS_A_DAY, '1 - 2 hours a day'),
    (INTERNET_TWO_TO_FOUR_HOURS_A_DAY, '2 - 4 hours a day'),
    (INTERNET_FOUR_TO_SIX_HOURS_A_DAY, '4 - 6 hours a Day'),
    (INTERNET_SIX_TO_EIGHT_HOURS_A_DAY, '6 - 8 hours a day'),
    (INTERNET_EIGHT_PLUS_HOURS_A_DAY, '8 + hours a day'),
           )

internet_usage = models.CharField(null=True, max_length=100, default=None, choices=INTERNET_USAGE, verbose_name='How long do you spend on the Internet each day?')

1 个答案:

答案 0 :(得分:3)

根据NickJ考虑​​答案,并使用RadioSelectNotNull,如下所示:

<强> forms.py

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)


class SurveyFormB(forms.ModelForm): 

    class Meta:
        model = Person
        fields = ['internet_usage', 'smart_phone_ownership', 'smart_phone_usage']        

        widgets = {'internet_usage' : RadioSelectNotNull,
                   'smart_phone_ownership' : forms.Select,
                   'smart_phone_usage' : RadioSelectNotNull,
                   }

请注意,如果您还可以将选项字段修改为:

<强> models.py

INTERNET_USAGE = (
    ("One", 'Less than one hour a day'),
    ( "Two", '1 - 2 hours a day'),
    ( "Three", '2 - 4 hours a day'),
    ( "Four", '4 - 6 hours a Day'),
    ( "Five", '6 - 8 hours a day'),
    ( "Six", '8 + hours a day'),
)

internet_usage = models.CharField(null=True, max_length=100, default=None, choices=INTERNET_USAGE, verbose_name='How long do you spend on the Internet each day?')