Friday, October 30, 2009

Tweaking ModelForms in django

I try to use a lot of ModelForms in django, it makes things simple and keeps information in once place. However, sometimes I need to change one property of a field. The docs just suggest you define the field again in the form but this means you have to redefine all aspects of the field. Here is an easier way:

class ProfileForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
super(ProfileForm, self).__init__(*args, **kwargs)

self.fields['details'].widget.attrs['cols'] = 100;
self.fields['details'].widget.attrs['rows'] = 12;

No comments:

Post a Comment