0

I have two TextField in my model. But i want to change the rows and cols attribute of only of the TextField. I know from this page that the look of TextField can be changed using the following code in the admin.py.

class RulesAdmin(admin.ModelAdmin):
formfield_overrides = {
    models.TextField: {'widget': Textarea(
                       attrs={'rows': 1,
                              'cols': 40})},
}

...
admin.site.register(Rules, RulesAdmin)

As I said, using the above code changed the look of both the TextField, I want only to change one of those here is how my model looks like:

class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    pub_date = models.DateTimeField(auto_now_add=True)
    update_date = models.DateTimeField(auto_now=True)
    meta = models.TextField(blank=True)

    def __str__(self):
        return self.title

I want only to change the look of meta field.

How should I go about this ?

Community
  • 1
  • 1
Cody
  • 2,480
  • 5
  • 31
  • 62
  • this [link](https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_overrides) will help you to customize `TextField`. – vinay kumar Feb 23 '17 at 18:00

1 Answers1

2

Add the following code to forms.py

from django.forms import ModelForm, Textarea
from .models import Lesson

class PostModelForm(ModelForm):

    class Meta:
        model = Lesson
        fields = ('__all__')
        widgets = {
            'meta': Textarea(attrs={'cols': 80, 'rows': 5}),
        }

and in admin.py do this

class PostModel(admin.ModelAdmin):
    list_display = ('id', 'title', 'pub_date', 'course',)
    search_fields = ('title', 'course__alias',)

    form = PostModelForm
Cody
  • 2,480
  • 5
  • 31
  • 62