Opened 16 years ago

Closed 15 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: 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 (last modified by Brian Rosner)

My models.py 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__) 
        super(StdHeader,self).save()       

    class Meta:
       abstract = True

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

I.E:

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/loader_tags.py", line 123, in render
    return t.render(context)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/__init__.py", line 176, in render
    return self.nodelist.render(context)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/__init__.py", 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/debug.py", 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/debug.py", line 71, in render_node
    result = node.render(context)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/defaulttags.py", line 148, in render
    nodelist.append(node.render(context))
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/debug.py", 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/__init__.py", line 535, in resolve
    obj = self.var.resolve(context)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/__init__.py", line 676, in resolve
    value = self._resolve_lookup(context)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/template/__init__.py", line 711, in _resolve_lookup
    current = current()
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/contrib/admin/helpers.py", line 134, in pk_field
    return AdminField(self.form, self.formset._pk_field.name, False)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/django/contrib/admin/helpers.py", 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/forms.py", 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 15 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 by Brian Rosner, 16 years ago

Description: modified (diff)

Fixed description formatting.

comment:2 by bromske, 15 years ago

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 models.py. The primary key fields will be inserted in the form as hidden fields.

by bromske, 15 years ago

Attachment: primkey_inline.diff added

comment:3 by Jacob, 15 years ago

Triage Stage: UnreviewedAccepted

comment:4 by jkocherhans, 15 years ago

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