Problem with inline form: "__init__() got an unexpected keyword argument 'instance'"

1,786 views
Skip to first unread message

Jop

unread,
Jan 12, 2010, 5:54:43 AM1/12/10
to Django users
Hello,

I struggeling with an error message for more than a day now and I
can't figure out what I'm doing wrong. I'm still kindof a newbie with
python/django so it might be very obvious..

I am trying to create an inline form and I want to hide a field in
this form (specific: I want to hide the pk field, for some reason
django doesn't hide this one when it has a different name than "id").
To achieve this I create a class which inherits admin.StackedInline.
In this class I define formset=modelformset_factory(ClientContact).

When trying to view te form in the admin I get an error, the Traceback
is below.

For testing purposes I made a very simple Client/Client model example,
the code is below.

I really appreciate any tips/suggestions/ideas :)

Greets Jop

model.py
==================================================================
from django.db import models

class Client(models.Model):
name = models.CharField(max_length=100)
city = models.CharField(max_length=100)

class ClientContact(models.Model):
contact_id = models.IntegerField("id", primary_key=True) #
my database requires this
title = models.CharField(max_length=250)
client = models.ForeignKey('Client')
==================================================================

admin.py
==================================================================
from testapp.models import *
from django.contrib import admin
from django.forms.models import modelformset_factory

class ClientContactInline(admin.StackedInline):
model = ClientContact
formset = modelformset_factory(ClientContact) # when I comment
this line out there is no problem

class ClientAdmin(admin.ModelAdmin):
inlines = [
ClientContactInline,
]

admin.site.register(Client, ClientAdmin)
==================================================================


TRACEBACK
==================================================================
Environment:

Request Method: GET
Request URL: http://testserver/djangocms/admin/testapp/client/1/
Django Version: 1.1.1
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.redirects',
'emailtool',
'contenttool',
'mainapp',
'testapp']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py"
in get_response
92. response = callback(request, *callback_args,
**callback_kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/sites.py"
in root
490. return self.model_page(request, *url.split('/',
2))
File "/usr/lib/python2.5/site-packages/django/views/decorators/
cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/sites.py"
in model_page
509. return admin_obj(request, rest_of_url)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/
options.py" in __call__
1098. return self.change_view(request, unquote(url))
File "/usr/lib/python2.5/site-packages/django/db/transaction.py" in
_commit_on_success
240. res = func(*args, **kw)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/
options.py" in change_view
847. formset = FormSet(instance=obj, prefix=prefix)
File "/usr/lib/python2.5/site-packages/django/forms/models.py" in
__init__
459. super(BaseModelFormSet, self).__init__(**defaults)

Exception Type: TypeError at /admin/testapp/client/1/
Exception Value: __init__() got an unexpected keyword argument
'instance'

Daniel Roseman

unread,
Jan 12, 2010, 6:17:07 AM1/12/10
to Django users
On Jan 12, 10:54 am, Jop <jop.reynt...@gmail.com> wrote:
> Hello,
>
> I struggeling with an error message for more than a day now and I
> can't figure out what I'm doing wrong. I'm still kindof a newbie with
> python/django so it might be very obvious..
>
> I am trying to create an inline form and I want to hide a field in
> this form (specific: I want to hide the pk field, for some reason
> django doesn't hide this one when it has a different name than "id").
> To achieve this I create a class which inherits admin.StackedInline.
> In this class I define formset=modelformset_factory(ClientContact).
>
> When trying to view te form in the admin I get an error, the Traceback
> is below.
>
> For testing purposes I made a very simple Client/Client model example,
> the code is below.
>
> I really appreciate any tips/suggestions/ideas :)
>
> Greets Jop


You probably need to use inlineformset_factory.

However, this isn't the way to define a custom form in an inline. Just
leave the formset as it is, but set the `form` property on the inline
class to the form you want to use in the formset.
--
DR.

Jop

unread,
Jan 12, 2010, 8:19:40 AM1/12/10
to Django users
Hi Daniel,

Thanx for the suggestions, i've got it working now! Up to the next
challenge :)

Greets Jop

Reply all
Reply to author
Forward
0 new messages