Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#1506 closed task (fixed)

[patch] Admin interfaces throws KeyError exception when using OneToOneFields as primary key

Reported by: wizeman Owned by: mtredinnick
Component: Core (Management commands) Version:
Severity: major Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The exception is thrown when someone tries to change a record in the admin interface that uses a OneToOneField as a primary key.

Exception details:

KeyError at /admin/main/consultores/36/
'id_user'
Request Method:	GET	
Request URL:	http://localhost:8000/admin/main/consultores/36/	
Exception Type:	KeyError	
Exception Value:	'id_user'	
Exception Location:	/usr/lib/python2.3/site-packages/django/core/meta/fields.py in original_value, line 75

Template error

In template /usr/lib/python2.3/site-packages/django/contrib/admin/templates/admin/field_line.html, error at line 14
Caught an exception while rendering.
4	{% for bound_field in bound_fields %}	
5	  {% if bound_field.has_label_first %}	
6	    {% field_label bound_field %}	
7	  {% endif %}	
8	  {% field_widget bound_field %}	
9	  {% if not bound_field.has_label_first %}	
10	    {% field_label bound_field %}	
11	  {% endif %}	
12	  {% if change %}	
13	    {% if bound_field.field.primary_key %}	
14	      {{ bound_field.original_value }}	
15	    {% endif %}	
16	    {% if bound_field.raw_id_admin %}	
17	      {% if bound_field.existing_display %}&nbsp;<strong>{{ bound_field.existing_display|truncatewords:"14" }}</strong>{% endif %}	
18	    {% endif %}	
19	  {% endif %}	
20	  {% if bound_field.field.help_text %}<p class="help">{{ bound_field.field.help_text }}</p>{% endif %}	
21	{% endfor %}	
22	</div>	
23

Traceback (innermost last)
Switch back to interactive view

Traceback (most recent call last):
File "/usr/lib/python2.3/site-packages/django/core/template/__init__.py" in render_node
  732. result = node.render(context)
File "/usr/lib/python2.3/site-packages/django/core/template/__init__.py" in render
  778. output = self.filter_expression.resolve(context)
File "/usr/lib/python2.3/site-packages/django/core/template/__init__.py" in resolve
  589. obj = resolve_variable(self.var, context)
File "/usr/lib/python2.3/site-packages/django/core/template/__init__.py" in resolve_variable
  677. current = current()
File "/usr/lib/python2.3/site-packages/django/core/meta/fields.py" in original_value
  76. return self.original.__dict__[self.field.column]

  KeyError at /admin/main/consultores/36/
  'id_user'

This is my model code:

class Utilizador(meta.Model):
	id_user = meta.AutoField('ID', primary_key = True)

	username = meta.CharField('Username', maxlength = 30, unique = True, null = True)
	nome = meta.CharField('Nome', maxlength = 50, unique = True)
	email = meta.EmailField('Email', unique = True, null = True)
	telemovel = meta.CharField('Nr Telefone', maxlength = 9, unique = True, null = True)

	class META:
		admin = meta.Admin(list_display = ('id_user', 'username', 'nome', 'email', 'telemovel'))
		db_table = 'users'
		module_name = 'utilizadores'
		verbose_name_plural = 'utilizadores'

	def __repr__(self):
		return self.nome

class Consultor(meta.Model):
	user = meta.OneToOneField(Utilizador, verbose_name = 'Utilizador', db_column = 'id_user')
	cod_consultor = meta.IntegerField('Codigo de consultor')

	class META:
		admin = meta.Admin(list_display = ('user', 'cod_consultor'))
		db_table = 'consultores'
		module_name = 'consultores'
		verbose_name_plural = 'consultores'

	def __repr__(self):
		return self.get_user().nome

Change History (6)

comment:1 Changed 9 years ago by wizeman

BTW, the bug is also present in Django 0.91.

I was able to solve it with the following change, but I don't know if it breaks anything else:

Index: django/core/meta/fields.py
===================================================================
--- django/core/meta/fields.py  (revision 2524)
+++ django/core/meta/fields.py  (working copy)
@@ -73,7 +73,7 @@

     def original_value(self):
         if self.original:
-            return self.original.__dict__[self.field.column]
+            return self.original.__dict__[self.field.attname]

     def __repr__(self):
         return "BoundField:(%s, %s)" % (self.field.name, self.form_fields)

comment:2 Changed 9 years ago by mir@…

  • Summary changed from Admin interfaces throws KeyError exception when using OneToOneFields as primary key to [patch] Admin interfaces throws KeyError exception when using OneToOneFields as primary key

comment:3 Changed 9 years ago by mtredinnick

  • Owner changed from adrian to mtredinnick

This has been fixed at some point in the interim. Will close it (along with #1064) once I find a place to put a regression test to ensure we don't break it again.

comment:4 Changed 9 years ago by mtredinnick

(In [3176]) Added regressions tests to ensure that one-to-one and many-to-many fields
continue to interact properly. Refs #1064. Refs #1506.

comment:5 Changed 9 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

comment:6 Changed 9 years ago by anonymous

  • Component changed from Admin interface to django-admin.py
  • priority changed from normal to lowest
  • Severity changed from normal to major
  • Type changed from defect to task
  • Version SVN deleted
Note: See TracTickets for help on using tickets.
Back to Top