Overriding ModelForm

1,214 views
Skip to first unread message

MrBodjangles

unread,
Apr 1, 2009, 3:35:20 AM4/1/09
to Django users
Hi All,
I know this is something very basic (perhaps even a typo), but the
code below gives an error:

" password = models.CharField(widget=PasswordInput())
TypeError: __init__() got an unexpected keyword argument 'widget'
"

( This should be similar to what was presented in the official doc:
>>> class ArticleForm(ModelForm):
... pub_date = DateField(widget=MyDateWidget())
)


The commented out line (validate_password) renders to type ="text",
and so I wanted to have it render as type="password"

---
from django.db import models
from django.forms import ModelForm
from django.forms.widgets import PasswordInput
from django import forms

# Create your models here.
class User_Account(models.Model):
username=models.CharField(max_length="24")
password = models.CharField(widget=PasswordInput())
# validate_password=models.CharField(max_length="10")
date_of_birth=models.DateTimeField()
email= models.EmailField(primary_key=True)

def __unicode__(self):
return self.name

class User_AccountForm(ModelForm):
class Meta:
model = User_Account

Daniel Roseman

unread,
Apr 1, 2009, 4:14:37 AM4/1/09
to Django users
On Apr 1, 8:35 am, MrBodjangles <MrAngelC...@gmail.com> wrote:
> Hi All,
> I know this is something very basic (perhaps even a typo), but the
> code below gives an error:
>
> "    password = models.CharField(widget=PasswordInput())
> TypeError: __init__() got an unexpected keyword argument 'widget'
> "
>
> ( This should be similar to what was presented in the official doc:>>> class ArticleForm(ModelForm):
>
> ...     pub_date = DateField(widget=MyDateWidget())
> )
>
> The commented out line (validate_password) renders to type ="text",
> and so I wanted to have it render as type="password"

You are confusing model and form fields. You can't set a widget on a
model field, only on a form field. If you want to override the widget
for the password field on the User_AccountForm, you'll have to
redefine that field on the form:

class User_AccountForm(ModelForm):
password = forms.CharField(widget=PasswordInput())
class Meta:
model = User_Account

--
DR.
Reply all
Reply to author
Forward
0 new messages