Question

@render_to('edit_operation.html')       
def edit_operation(request, pk):
    from forms import OpBaseForm, OperationForm
    from django.forms.models import inlineformset_factory

    op = Operation.objects.get(pk=pk)

    OpBaseFormSet = inlineformset_factory(Operation, OpBase, form=OpBaseForm, extra=5, )

    if request.method == "POST":
        form    = OperationForm(request.POST, instance=op)
        formset = OpBaseFormSet(request.POST, instance=op)

        if formset.is_valid() and form.is_valid():
            form.save()
            formset.save()   #error here

            return HttpResponseRedirect( op.get_absolute_url() )
    else:
        form    = OperationForm(instance=op)
        formset = OpBaseFormSet(instance=op)


    return {'operation': op, 'opform': form, 'formset': formset}

This seems pretty straightforward, but whenever I try to submit data (even unchanged data), I get this error:

Environment:

Request Method: POST
Request URL: http://localhost:8000/edit/operation/1/
Django Version: 1.0.2 final
Python Version: 2.6.2
Installed Applications:
['main',
 'django_extensions',
 'django.contrib.comments',
 'django.contrib.admindocs',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.gis',
 'django.contrib.humanize',
 'registration']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "/usr/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  86.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.6/dist-packages/annoying/decorators.py" in wrapper
  55.             output = function(request, *args, **kwargs)
File "/home/chris/Websites/jobmap/main/views.py" in edit_operation
  68.           formset.save()
File "/usr/lib/python2.6/dist-packages/django/forms/models.py" in save
  389.         return self.save_existing_objects(commit) + self.save_new_objects(commit)
File "/usr/lib/python2.6/dist-packages/django/forms/models.py" in save_existing_objects
  403.             obj = existing_objects[form.cleaned_data[self._pk_field.name]]

Exception Type: KeyError at /edit/operation/1/
Exception Value: None

Anyone have any idea what is causing this error? If theres an error in the formset, then what is formset.is_valid() doing being 'True'? Anyone at least have any ideas on how to debug this in order to find whats going wrong? Both forms work perfectly fine when used individually, and I have {{formset.management_form}} in my template.

Was it helpful?

Solution

Just a quick guess... is your OpBaseForm excluding the model pk? Or, are you missing the pk field in your template? In other words, if you are laying out the fields yourself you still need to include the pk field even though it's hidden.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top