Error - got an unexpected keyword argument 'instance' on initialising form

6,090 views
Skip to first unread message

phoebebright

unread,
Jul 28, 2009, 2:02:29 PM7/28/09
to Django users
I thought I was faithfully coping the tutorial, so don't understand
why I get this error.

Here is the code in the view:

@login_required
def edit_todo(request, todo_id):

task = Task.objects.get(id=todo_id)

if request.method == 'POST':
form = TaskForm(request.POST)
...

else:
form1 = TaskForm() # this is ok
form2 = TaskForm(instance=task) #this fails with error

__init__() got an unexpected keyword argument 'instance'

Form is simple:

class TaskForm(forms.Form):
owner = forms.ModelChoiceField(Who.objects.filter(owner=1),
empty_label=None, required=True)
name = forms.CharField(max_length=140, required=True)
taglist = forms.CharField(max_length=100, required=False)
description = forms.CharField(widget=forms.Textarea,
required=False)

class Media:
js = ('/js/jquery.js', '/js/jquery-ui.min.js','js/
jeditable.js',)
css = { 'all': ('/css/jquery-ui.css','/css/smoothness/jquery-
ui-1.7.2.custom.css') }

class Meta :
model = Task

??? Is it something obvious again?

Alex Gaynor

unread,
Jul 28, 2009, 2:06:02 PM7/28/09
to django...@googlegroups.com
It looks like you intended to use a ModelForm, but instead inherited
from Form, change you form to subclass forms.ModelFrom.

Alex

--
"I disapprove of what you say, but I will defend to the death your
right to say it." -- Voltaire
"The people's good is the highest law." -- Cicero
"Code can always be simpler than you think, but never as simple as you
want" -- Me

phoebebright

unread,
Jul 28, 2009, 2:12:53 PM7/28/09
to Django users
That would be it! Thanks for your prompt help - onwards....

On Jul 28, 7:06 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages