本文介绍了Django-Admin:作为 TextArea 的 CharField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

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

class Cab_Admin(admin.ModelAdmin):
    ordering     = ('name',)
    list_display = ('name','descr', )
    # what to write here to make descr using TextArea?

admin.site.register( Cab, Cab_Admin )

如何在管理界面中将 TextArea 小部件分配给descr"字段?

how to assign TextArea widget to 'descr' field in admin interface?

更新:
仅在管理界面中!

使用 ModelForm 的好主意.

Good idea to use ModelForm.

推荐答案

你必须创建一个 forms.ModelForm 来描述你希望 descr 字段如何被显示,然后告诉 admin.ModelAdmin 使用该表单.例如:

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

admin.ModelAdminform 属性记录在 Django 官方文档中.这是 一个地方看看在.

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

这篇关于Django-Admin:作为 TextArea 的 CharField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:51