Cody
Cody

Reputation: 2649

How to customize a specific TextField inside the Django admin not all?

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 ?

Upvotes: 0

Views: 4737

Answers (1)

Cody
Cody

Reputation: 2649

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

Upvotes: 2

Related Questions