覆盖Django中的默认字段类型

时间:2011-02-08 06:45:40

标签: python django django-forms

models.py

class Job(db.Model):

title = db.StringProperty(verbose_name='Project Title:')
description = db.StringProperty(multiline=True, verbose_name='Description:')

created_at = db.DateTimeProperty(auto_now_add=True)
updated_at = db.DateTimeProperty(auto_now=True)
budget = db.IntegerProperty()
max_project_duration = db.IntegerProperty()

forms.py

class JobForm(djangoforms.ModelForm):

def __init__ (self, *args, **kwargs):
    super(JobForm, self).__init__(*args, **kwargs)
    self.fields['description'].widget.attrs['rows'] = '2'
    self.fields['description'].widget.attrs['cols'] = '70' 

class Meta:
    model = Job

我想将标题的长度设置为更长的宽度。我不确定如何操纵小部件。有人知道吗?

1 个答案:

答案 0 :(得分:1)

以下是我在Django 1.2中的表现:

from django.forms import ModelForm
from django.forms.widgets import Textarea, TextInput

from my_project.models import Job

class JobForm(ModelForm):

    class Meta:
        model = Job
        widgets = {
          'description': Textarea(attrs={'rows': '2', 'cols': '70}),
          'title': TextInput(attrs={'size': '60'}),
        }

这记录在Widgets page in the Django docs的底部。