Opened 16 years ago

Closed 16 years ago

Last modified 12 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: dev
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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):
        return self.name

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' % (self.name, unicode(self.parent))

and this admin.py:

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',)

admin.site.register(Parent, ParentAdmin)
admin.site.register(Child, 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:

Environment:

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:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'django.contrib.redirects',
 'playground.inlinet1']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware',
 'django.middleware.transaction.TransactionMiddleware')


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

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 by Jacob, 16 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Brian Rosner, 16 years ago

Owner: changed from nobody to Brian Rosner
Status: newassigned

comment:3 by jonloyens, 16 years ago

I have filed a related issue here: 8719

comment:4 by Brian Rosner, 16 years ago

Resolution: fixed
Status: assignedclosed

Fixed in [8756].

comment:5 by Jacob, 12 years ago

milestone: 1.0

Milestone 1.0 deleted

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