views:

1373

answers:

5

I have

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 )

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

upd:
In Admin interface only!

Good idea to use ModelForm.

A: 

Instead of a models.CharField, use a models.TextField for descr.

mipadi
Unfortunately, max_length= is totally ignored by Django on a TextField, so when you need field length validation (such as letting secretaries enter event summaries) the only way to get it is to use a CharField. But a CharField is way to short to type 300 chars into. Hence the ayaz solution.
shacker
+8  A: 

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.ModelForm is documented in the official Django documentation. Here is one place to look at.

ayaz
+3  A: 

You can subclass your own field with needed formfield method:

class CharFieldWithTextarea(models.CharField):
    def formfield(self, **kwargs):
         kwargs.update(
            "widget": forms.Textarea
         )
         return super(CharFieldWithTextarea, self).formfield(**kwargs)

This will take affect on all generated forms.

Alex Koshelev
+5  A: 

For this case, the best option is probably just to use a TextField instead of CharField in your model. You can also override formfield_for_dbfield method of ModelAdmin class:

class CabAdmin(admin.ModelAdmin):
    def formfield_for_dbfield(self, db_field, **kwargs):
        formfield = super(CabAdmin, self).formfield_for_dbfield(db_field, **kwargs)
        if db_field.name == 'descr':
            formfield.widget = forms.Textarea()
        return formfield
Carl Meyer
+3  A: 

just a note about ayaz's answer for anyone who comes across this.

this line

class Cab_Admin( admin.ModelForm ):

i believe should be

class Cab_Admin( admin.ModelAdmin ):

in django 1.0.2 at least

tosh
Add that as a comment, perhaps?
anonymous coward
That would make more sense, but i don't have enough "reputation" to comment. Perhaps only on my own posts?
tosh