Django

Code

Ticket #1506 (closed: fixed)

Opened 3 years ago

Last modified 2 years ago

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

Reported by: wizeman Assigned to: mtredinnick
Milestone: Component: django-admin.py
Version: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

Attachments

Change History

03/16/06 16:01:48 changed 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)

04/21/06 03:41:50 changed by mir@noris.de

  • 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.

06/18/06 20:46:59 changed 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.

06/20/06 00:29:19 changed 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.

06/20/06 00:29:41 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

07/02/06 11:47:35 changed by anonymous

  • priority changed from normal to lowest.
  • type changed from defect to task.
  • version deleted.
  • component changed from Admin interface to django-admin.py.
  • severity changed from normal to major.

Add/Change #1506 ([patch] Admin interfaces throws KeyError exception when using OneToOneFields as primary key)




Change Properties
Action