﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
7362	Admin model editor executes thousands of SQL commands when foreign key referenced in __str__	Andy Pavlo <Andrew_Pavlo@…>	nobody	"I have three models in my application:

{{{
class State(models.Model):
   name = models.CharField(max_length=20,unique=True)
   code = models.USStateField()
   def __str__(self):
        return self.name

class Location(models.Model):
   zipcode = models.CharField(max_length=5,primary_key=True,core=True)
   state = models.ForeignKey(State,core=True)
   city = models.CharField(max_length=64,core=True)
   def __str__(self):
      return self.city + ', ' + str(self.state.code) + str(self.zipcode)

class Company(models.Model):
   name = models.CharField(max_length=64,unique=True)
   address = models.CharField(max_length=128)
   location = models.ForeignKey(Location,null=False,blank=True)
   def __str__(self):
      return self.name
}}}

When I try to edit a company record, the page hangs forever because in the background Django is executing a lookup query on the state. The following is from my MySQL 5.0.45 server's query log:

{{{
<SNIP>
3274 Query       SELECT `generator_state`.`id`,`generator_state`.`name`,`generator_state`.`code` FROM `generator_state` WHERE (`generator_state`.`id` = 46)
3274 Query       SELECT `generator_state`.`id`,`generator_state`.`name`,`generator_state`.`code` FROM `generator_state` WHERE (`generator_state`.`id` = 46)
3274 Query       SELECT `generator_state`.`id`,`generator_state`.`name`,`generator_state`.`code` FROM `generator_state` WHERE (`generator_state`.`id` = 46)
3274 Query       SELECT `generator_state`.`id`,`generator_state`.`name`,`generator_state`.`code` FROM `generator_state` WHERE (`generator_state`.`id` = 46)
3274 Query       SELECT `generator_state`.`id`,`generator_state`.`name`,`generator_state`.`code` FROM `generator_state` WHERE (`generator_state`.`id` = 50)
3274 Query       SELECT `generator_state`.`id`,`generator_state`.`name`,`generator_state`.`code` FROM `generator_state` WHERE (`generator_state`.`id` = 50)
<SNIP>
}}}

If I remove the {{{__str__()}}} method from the Location model, then the admin editor form loads just fine!

There are a couple weird things about this problem. First is that the admin listing page works fine, as well using the model in the actual application). The second weird thing is that that the state_id used in SQL lookups seem to be random -- that is they don't reference the state used in the Location record. Lastly, is that I still have this problem even if Company.location is not included in the Admin.fields property.

I'm not sure if this is a genuine bug, or if this something that somebody has said ""don't do that"" before.

I am using py25-django-devel-20080404 installed on FreeBSD 6.2"		closed	contrib.admin	dev		wontfix			Unreviewed	0	0	0	0	0	0
