如何更改empty_label for modelForm选项字段? [英] How to change empty_label for modelForm choice field?
问题描述
我在我的一个模型中有一个字段,如下所示:
I have a field in one of my models like the following:
payrollProvider = models.CharField(max_length=2, choices=PAYROLL_CHOICES)
PAYROLL_CHOICES = (
('C1', 'Choice1'),
('C2', 'Choice2')
etc.....
)
当我为此字段创建模型表单时,Django正确生成一个HTML选择框,但是包含一个默认的空白值---------。
When I create a model form for this field, Django correctly generates an HTML select box, but includes a default blank value of "---------".
我想知道如何将这个默认值更改为其他一些文本,例如请选择价值。
I would like to know how to change this default value to some other text, such as "please choose value".
我相信我应该能够通过以下内容在我的模型窗体的 init 中设置在这个答案和其他几个: / p>
I believe I should be able to set this in my model form's init via the following, as documented in this answer and several others:
self.fields['payrollProvider'].empty_label = "please choose value"
但是,这对我来说不起作用。当我在表单的 init 中包含该行时,--------仍然显示为选择框中的初始选项。我正在粘贴以下相关的forms.py,但其他人似乎也无法访问/修改empty_label 。在这个链接中,提问者描述了一种方法来删除默认的empty_label值(我可以通过他的方法成功地完成),但我真正想做的是修改显示的empty_label。
However, this isn't working for me. When I include that line in my form's init, "--------" still shows up as the initial choice in the select box. I'm pasting the relevant forms.py below, but it seems that others have also been unable to access / modify empty_label. At this link, the questioner describes a way to delete the default empty_label value (which I was able to do successfully via his method) but what I really want to do is to modify the empty_label that is displayed.
任何想法?
以下是forms.py表单的代码,而empty_label代码在更改默认的----------时不成功:
Here's the code for the form in forms.py, with the empty_label code that isn't successful at changing the default "----------":
class PayrollCredentialForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(PayrollCredentialForm, self).__init__(*args, **kwargs)
self.fields['payrollUsername'].widget.attrs.update({'class' : 'yp-signup'})
self.fields['payrollPassword'].widget.attrs.update({'class' : 'yp-signup'})
self.fields['payrollProvider'].widget.attrs.update({'class' : 'yp-signup'})
self.fields['payrollUsername'].widget.attrs.update({'placeholder' : ' Payroll Username'})
self.fields['payrollPassword'].widget.attrs.update({'placeholder' : ' Payroll Password'})
self.fields['payrollProvider'].empty_label = "please choose value"
class Meta:
model = Company
fields = ('payrollProvider', 'payrollUsername', 'payrollPassword')
widgets = {
'payrollPassword': forms.PasswordInput(),
}
推荐答案
问题是您正在尝试指定不适用于选择字段类型的内容。
The problem is that you are trying to specify something that is not available for the type of Select field.
empty_label选项用于forms.ModelChoiceField,这恰好是使用选择窗口小部件,但与您提供选项的CharField不同的字段。
The empty_label option is for forms.ModelChoiceField, which happens to use a Select widget, but is not the same kind of field as your CharField that you are providing options for.
https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield
您也可以在上一个问题中看到这里: https://stackoverflow.com/a/740011/1406860
You can see this also in a previous question here: https://stackoverflow.com/a/740011/1406860
您可以尝试覆盖modelform的html,将第一个选项添加为请选择值。或者,您可以使用模板过滤器来做同样的事情。最后,您可以和(,请选择价值)给PAYROLL_CHOICES,如果您不希望在没有payrollProvider的情况下提交它,则只为模型中的字段设置blank = False。
You could try and override the html of the modelform to add the first option as "please choose value". Alternatively, you could use a template filter to do the same thing. Lastly, you could and ("", "please choose value") to PAYROLL_CHOICES, and if you don't want it to be submitted without a payrollProvider just set blank=False for the field in the model.
JD
这篇关于如何更改empty_label for modelForm选项字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!