我的代码设置如下:
models.py
class Category(models.Model):
name = models.CharField(max_length=255)
class Product(models.Model):
category = models.ForeignKey(Category)
forms.py
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = ('category',)
widgets = {
#'category' : forms.TextInput(attrs={'placeholder' : 'Select the category'}),
}
基本上,现在,没有小部件部分,我得到 --------
作为该字段的占位符。但是,我想要“选择类别”,我该怎么做?谢谢!
最佳答案
您可以使用 empty_label 设置自定义占位符字段:
class ProductForm(forms.ModelForm):
category = forms.ModelChoiceField(queryset=Category.objects.all(), empty_label='Select the category')
class Meta:
model = Product
关于python - 更改 Django 模型表单中外键的默认占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43710970/