컬럼 사이즈 조정

2021. 1. 15. 21:10파이썬/Django

from django.forms import Textarea

# 모든 TextField에 적용
    formfield_overrides = {
        models.TextField: {'widget': Textarea(attrs={'rows': 1,'cols': 40})},
    }

# 특정 컬럼에 적용
    def formfield_for_dbfield(self, db_field, **kwargs):
        field = super(GanntAdmin, self).formfield_for_dbfield(db_field, **kwargs)
        if db_field.name == 'harf':
            field.widget = forms.Textarea(attrs={'rows': 2,'cols': 40})
            #field.widget.attrs['class'] = 'someclass ' + field.widget.attrs.get('class', '')
        return field