2

在我的模型中,我有一个 CharField 有几个选择:

SURFACE_CHOICES = (
        ('as', _('Asphalt')),  
        ('co', _('Concrete')), 
        ('ot', _('Other')), )
surface = models.CharField(choices = ROOF_SURFACE_CHOICES, 
        max_length = 2, blank = True, null = True,)

我想避免-------在表单的下拉框中进行选择,并将其替换为更具描述性的文本。

__init__ method我曾经用以下行来做forms.py

def __init__(self, auto_id='%s', *args, **kwargs):
    super(SurfaceUpdateForm, self).__init__(*args, **kwargs)
    self.fields['surface'].choices.insert(0,('', _('Please choose a surface')))

但是,在我当前的应用程序中,第一行仍然存在-------,并且没有被上述文本替换。有没有更好的方法来设置空白值?

4

1 回答 1

0

The ------- appears because your field has blank=True, that is, it is optional. This Django mechanics is kinda hard to understand, but you can override the choices:

def __init__(self, auto_id='%s', *args, **kwargs):
    super(SurfaceUpdateForm, self).__init__(*args, **kwargs)
    self.fields['surface'].choices = [('', _('Please choose a surface'))] + MyModel.SURFACE_CHOICES
于 2013-09-04T19:54:34.013 回答