Opened 10 years ago

Closed 10 years ago

Last modified 7 years ago

#8695 closed (fixed)

KeyError on save of model with inline-edited OneToOne sharing primary key

Reported by: Karen Tracey Owned by: Brian Rosner
Component: contrib.admin Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


This ticket is an attempt to state one simple re-creatable problem so as to simplify the tangled issues raised in #8241 and #8562.

Given these models:

from django.db import models

# Create your models here.
class Parent(models.Model):
    name = models.CharField(max_length=50)
    def __unicode__(self):

class Child(models.Model):
    name = models.CharField(max_length=50)
    parent = models.OneToOneField(Parent, primary_key=True)
    def __unicode__(self):
        return '%s, child of %s' % (, unicode(self.parent))

and this

from django.contrib import admin
from inlinet1.models import Parent, Child

class ChildInline(admin.StackedInline):
    model = Child
    max_num = 1
class ParentAdmin(admin.ModelAdmin):
    inlines = [ChildInline]
    list_display = ('name',)

class ChildAdmin(admin.ModelAdmin):
    list_display = ('name', 'parent',), ParentAdmin), ChildAdmin)

From the admin:

1 - Select Add on Parents, fill in parent name & inline-edited child name, select "Save and continue editing"

2 - Select "Save and continue editing" again and you'll get an exception:


Request Method: POST
Request URL: http://localhost:8888/admin/inlinet1/parent/3/
Django Version: 1.0-beta_2-SVN-8712
Python Version: 2.5.1
Installed Applications:
Installed Middleware:

File "/home/kmt/tmp/django/trunk/django/core/handlers/" in get_response
  86.                 response = callback(request, *callback_args, **callback_kwargs)
File "/home/kmt/tmp/django/trunk/django/contrib/admin/" in root
  173.                 return self.model_page(request, *url.split('/', 2))
File "/home/kmt/tmp/django/trunk/django/views/decorators/" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)
File "/home/kmt/tmp/django/trunk/django/contrib/admin/" in model_page
  192.         return admin_obj(request, rest_of_url)
File "/home/kmt/tmp/django/trunk/django/contrib/admin/" in __call__
  196.             return self.change_view(request, unquote(url))
File "/home/kmt/tmp/django/trunk/django/db/" in _commit_on_success
  238.                 res = func(*args, **kw)
File "/home/kmt/tmp/django/trunk/django/contrib/admin/" in change_view
  588.                     self.save_formset(request, form, formset, change=True)
File "/home/kmt/tmp/django/trunk/django/contrib/admin/" in save_formset
File "/home/kmt/tmp/django/trunk/django/forms/" in save
  280.         return self.save_existing_objects(commit) + self.save_new_objects(commit)
File "/home/kmt/tmp/django/trunk/django/forms/" in save_existing_objects
  294.             obj = existing_objects[form.cleaned_data[]]

Exception Type: KeyError at /admin/inlinet1/parent/3/
Exception Value: 'parent_id'

The changes to add_fields in the patches for #8562 fix the error, but perhaps not in the correct fashion.

Change History (5)

comment:1 Changed 10 years ago by Jacob

Triage Stage: UnreviewedAccepted

comment:2 Changed 10 years ago by Brian Rosner

Owner: changed from nobody to Brian Rosner
Status: newassigned

comment:3 Changed 10 years ago by jonloyens

I have filed a related issue here: 8719

comment:4 Changed 10 years ago by Brian Rosner

Resolution: fixed
Status: assignedclosed

Fixed in [8756].

comment:5 Changed 7 years ago by Jacob

milestone: 1.0

Milestone 1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top