Opened 10 years ago

Closed 9 years ago

#8748 closed (fixed)

Problems in admin/form with primary keys in an abstract superclass

Reported by: trott@… Owned by: nobody
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: no UI/UX: no

Description (last modified by Brian Rosner)

My has an abstract superclass, which defines (beside other things) the primary key (as a UID):

class StdHeader(models.Model):
    Standard header. Each database relation starts with these attributes.
    pkey    = models.CharField(max_length=40, primary_key=True, editable=True)        # primary key; an UID

    def save(self):
        """update of all std header attributes"""
        if self.pkey == None or len(self.pkey)==0:
            self.pkey = uids.uid(self.__class__.__name__) 

    class Meta:
       abstract = True

Now: in later SVN versions (specifically in SVN 8785) there are "KeyErrors"
in dependend Models:


class Person(StdHeader):
    familyname   = models.CharField(_('Familyname'),max_length=30)

class Address(StdHeader):
    person           = models.ForeignKey(Person,           null=True, to_field="pkey")
    street      = models.CharField(max_length=40, blank=True)

Adding a record to Person results in:

  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/", line 123, in render
    return t.render(context)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/", line 176, in render
    return self.nodelist.render(context)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/", line 768, in render
    bits.append(self.render_node(node, context))
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/", line 81, in render_node
    raise wrapped
TemplateSyntaxError: Caught an exception while rendering: "Key 'pkey' not found in Form"

Original Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/", line 71, in render_node
    result = node.render(context)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/", line 148, in render
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/", line 87, in render
    output = force_unicode(self.filter_expression.resolve(context))
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/", line 535, in resolve
    obj = self.var.resolve(context)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/", line 676, in resolve
    value = self._resolve_lookup(context)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/", line 711, in _resolve_lookup
    current = current()
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/contrib/admin/", line 134, in pk_field
    return AdminField(self.form,, False)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/contrib/admin/", line 77, in __init__
    self.field = form[field] # A django.forms.BoundField instance
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/forms/", line 105, in __getitem__
    raise KeyError('Key %r not found in Form' % name)
KeyError: "Key 'pkey' not found in Form"

Note: There might be a relation to ticket #8562 as i got the same trace when editing/saving.

Attachments (1)

primkey_inline.diff (1013 bytes) - added by bromske 10 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 10 years ago by Brian Rosner

Description: modified (diff)

Fixed description formatting.

comment:2 Changed 10 years ago by bromske

I've same problems here, i think there is a problem with own primary keys and inlines in the admin/form view.

I found 2 solutions to solve the problem.

  1. Set the primary key fields editable=True, but then the values can mofified manuelly in the form. :(
  1. A little change to the The primary key fields will be inserted in the form as hidden fields.

Changed 10 years ago by bromske

Attachment: primkey_inline.diff added

comment:3 Changed 10 years ago by Jacob

Triage Stage: UnreviewedAccepted

comment:4 Changed 9 years ago by jkocherhans

Resolution: fixed
Status: newclosed

This is working in the most recent version of trunk. r10283. I removed editable=True from your pkey field and used str(random.random()) to generate its value in save. If you have amore specific set of models that triggers this issue, please re-open this ticket.

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