如下代码,Account 模型中有个CharField类型的字段,对应在Admin中的是input输入框。如果我们想在admin中希望它是textarea输入框,那么需要自定义一个form表单对象
class Account(BaseModel):
c_remark = models.CharField(max_length=200, null=True, blank=True, verbose_name="备注")
首先,创建自定义Form 对象 AccountModelForm, 指定 c_remark 字段的 widget 属性为forms.Textarea
from django import forms
from .models import Account
class AccountModelForm(forms.ModelForm):
c_remark = forms.CharField(widget=forms.Textarea, label="备注", required=False)
class Meta:
model = Account
fields = '__all__'
然后,在admin中指定自定义的AccountAdmin
@admin.register(Account)
class AccountAdmin(admin.ModelAdmin):
form = AccountModelForm
关注公众号「Python之禅」,回复「1024」免费获取Python资源