开发者

Changing ForeignKey's defaults in admin site

开发者 https://www.devze.com 2022-12-18 20:04 出处:网络
I\'d like to know how I can change the blank value of ForeignKey in admin site\'s forms. There blank is showed as \"-----\". I wanna replace it by a开发者_运维百科 word.

I'd like to know how I can change the blank value of ForeignKey in admin site's forms. There blank is showed as "-----". I wanna replace it by a开发者_运维百科 word.

Does someone know how to do it?


Create custom ModelForm and override your field there, then, assign this form class to form option of ModelAdmin. Like this:

#forms.py
class CustomForm(forms.ModelForm):
    user = forms.ModelChoiceField(queryset=User.objects.all(), empty_label=u'label')

class Meta:
    model = MyModel

#admin.py
class MyModelAdmin(admin.ModelAdmin):
     form = CustomForm


You need to use ModelChoiceField's empty_label.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号