Django-Admin: CharField as TextArea

前端 未结 9 2068
一个人的身影
一个人的身影 2020-12-07 09:20

I have

class Cab(models.Model):
    name  = models.CharField( max_length=20 )
    descr = models.CharField( max_length=2000 )

class Cab_Admin(admin.ModelAd         


        
9条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 10:04

    You will have to create a forms.ModelForm that will describe how you want the descr field to be displayed, and then tell admin.ModelAdmin to use that form. For example:

    from django import forms
    class CabModelForm( forms.ModelForm ):
        descr = forms.CharField( widget=forms.Textarea )
        class Meta:
            model = Cab
    
    class Cab_Admin( admin.ModelAdmin ):
        form = CabModelForm
    

    The form attribute of admin.ModelAdmin is documented in the official Django documentation. Here is one place to look at.

提交回复
热议问题