2011-02-08 148 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的底部記錄。