Lynn Chang
Lynn Chang

Reputation: 79

Overriding the default field type in Django

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

I want to set the length for title to a longer width. I'm not sure how to manipulate the widget. Anyone knows?

Upvotes: 0

Views: 214

Answers (1)

gravelpot
gravelpot

Reputation: 1767

Here's how I would do it in 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'}),
        }

This is documented at the bottom of the Widgets page in the Django docs.

Upvotes: 1

Related Questions