![](/img/trans.png)
[英]How to change in Django Admin the default form (dropdown list) for a foreignKey model?
[英]Changing the default placeholder in a Django model form for a ForeignKey
我的代碼設置如下:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.