overriding __init__ in forms.ModelForm?

1,161 views
Skip to first unread message

Pryankster

unread,
Aug 10, 2008, 7:23:07 PM8/10/08
to Django users, pryank...@gmail.com
I am trying to override the __init__() function of a ModelForm derived
class, but when I try to add the __init__() function to the ModelForm
derived class, I get an error (included below). I have reduced the
problem to the existence of the __init__() method in the ModelForm
dervied class, if I remove the __init__() method, or rename it to
something else, everything works (though, obviously, it's not
called). I suspect it has something to do with the way metaclasses
are implemented that I just don't understand.

Here is the code (distilled down to as simple of an example as
possible):

from django.db import models
from django import forms
from django.contrib import admin
import sys

class Show(models.Model):
title = models.CharField(max_length = 100)

def __unicode__(self):
return self.title

class ShowForm(forms.ModelForm):
def __init__(self,*args,**kwargs):
print >>sys.stderr, "got to __init__"
super(ShowForm,self).__init__(self,*args,**kwargs)

class Meta:
model = Show

class ShowAdmin(admin.ModelAdmin):
form = ShowForm

admin.site.register(Show,ShowAdmin)

and here's the traceback:

Traceback:
File "/usr/local/lib/python2.4/site-packages/django/core/handlers/
base.py" in get_response
86. response = callback(request, *callback_args,
**callback_kwargs)
File "/usr/local/lib/python2.4/site-packages/django/contrib/admin/
sites.py" in root
156. return self.model_page(request, *url.split('/',
2))
File "/usr/local/lib/python2.4/site-packages/django/views/decorators/
cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.4/site-packages/django/contrib/admin/
sites.py" in model_page
173. return admin_obj(request, rest_of_url)
File "/usr/local/lib/python2.4/site-packages/django/contrib/admin/
options.py" in __call__
286. return self.change_view(request, unquote(url))
File "/usr/local/lib/python2.4/site-packages/django/contrib/admin/
options.py" in change_view
602. context = {
File "/usr/local/lib/python2.4/site-packages/django/contrib/admin/
options.py" in __init__
831. self.extend(form.errors.values())
File "/usr/local/lib/python2.4/site-packages/django/forms/forms.py" in
_get_errors
111. self.full_clean()
File "/usr/local/lib/python2.4/site-packages/django/forms/forms.py" in
full_clean
212. value = field.widget.value_from_datadict(self.data,
self.files, self.add_prefix(name))
File "/usr/local/lib/python2.4/site-packages/django/forms/widgets.py"
in value_from_datadict
168. return data.get(name, None)

Exception Type: AttributeError at /admin/forminherit/show/1/
Exception Value: 'ShowForm' object has no attribute 'get'

Julien Phalip

unread,
Aug 10, 2008, 10:27:31 PM8/10/08
to Django users
Hi,

You should remove the 'self' when calling the parent's __init__:

super(ShowForm,self).__init__(*args,**kwargs)

Regards,

Julien

Pryankster

unread,
Aug 11, 2008, 12:17:58 AM8/11/08
to Django users
Yeah, I found that about 10 minutes after I sent the mail message :-(

Sometime you can't see the forest for the trees.

-- pryankster
Reply all
Reply to author
Forward
0 new messages