私信  •  关注

emyller

emyller 最近创建的主题
emyller 最近回复了
11 年前
回复了 emyller 创建的主题 » 自定义/删除Django选择框空白选项

我今天把这件事搞得一团糟,就想出了一个 胆小鬼 俏皮的解决方案:

# Cowardly handle ModelChoiceField empty label
# we all hate that '-----' thing
class ModelChoiceField_init_hack(object):
    @property
    def empty_label(self):
        return self._empty_label

    @empty_label.setter
    def empty_label(self, value):
        self._empty_label = value
        if value and value.startswith('-'):
            self._empty_label = 'Select an option'
ModelChoiceField.__bases__ += (ModelChoiceField_init_hack,)

现在您可以调整默认值 ModelChoiceField 任何你想要的东西都要空标签。-)

附言:不需要投反对票,无害的猴子补丁总是很方便的。