Django-Admin: CharField as TextArea

前端 未结 9 2048
一个人的身影
一个人的身影 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:03

    If you are trying to change the Textarea on admin.py, this is the solution that worked for me:

    from django import forms
    from django.contrib import admin
    from django.db import models
    from django.forms import TextInput, Textarea
    
    from books.models import Book
    
    class BookForm(forms.ModelForm):
        description = forms.CharField( widget=forms.Textarea(attrs={'rows': 5, 'cols': 100}))
        class Meta:
            model = Book
    
    class BookAdmin(admin.ModelAdmin):
        form = BookForm
    
    admin.site.register(Book, BookAdmin)
    

    If you are using a MySQL DB, your column length will usually be autoset to 250 characters, so you will want to run an ALTER TABLE to change the length in you MySQL DB, so that you can take advantage of the new larger Textarea that you have in you Admin Django site.

提交回复
热议问题