2011-02-08 146 views
0

models.py覆盖默认字段类型在Django

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

这是我会怎么做在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的底部记录。