Django Admin Running Same Query Thousands of Times for Model

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-03-27T16:51:04Z Indexed on 2010/03/27 16:53 UTC
Read the original article Hit count: 150

Filed under:
|
|

Running into an odd . . . loop when trying to view a model in the Django admin. I have three related models (code trimmed for brevity, hopefully I didn't trim something I shouldn't have):

class Association(models.Model):
    somecompany_entity_id = models.CharField(max_length=10, db_index=True)
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

class ResidentialUnit(models.Model):
    building = models.CharField(max_length=10)
    app_number = models.CharField(max_length=10)
    unit_number = models.CharField(max_length=10)
    unit_description = models.CharField(max_length=100, blank=True)
    association = models.ForeignKey(Association)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return '%s: %s, Unit %s' % (self.association, self.building, self.unit_number)

class Resident(models.Model):
    unit = models.ForeignKey(ResidentialUnit)
    type = models.CharField(max_length=20, blank=True, default='')
    lookup_key = models.CharField(max_length=200)
    jenark_id = models.CharField(max_length=20, blank=True)
    user = models.ForeignKey(User)
    is_association_admin = models.BooleanField(default=False, db_index=True)
    show_in_contact_list = models.BooleanField(default=False, db_index=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    _phones = {}
    home_phone = None
    work_phone = None
    cell_phone = None
    app_number = None
    account_cache_key = None

    def __unicode__(self):
        return '%s' % self.user.get_full_name()

It's the last model that's causing the problem. Trying to look at a Resident in the admin takes 10-20 seconds. If I take 'self.association' out of the __unicode__ method for ResidentialUnit, a resident page renders pretty quickly. Looking at it in the debug toolbar, without the association name in ResidentialUnit (which is a foreign key on Resident), the page runs 14 queries. With the association name put back in, it runs a far more impressive 4,872 queries. The strangest part is the extra queries all seem to be looking up the association name. They all come from the same line, the __unicode__ method for ResidentialUnit. Each one is the exact same thing, e.g.,

SELECT `residents_association`.`id`, `residents_association`.`jenark_entity_id`, `residents_association`.`name` FROM `residents_association` WHERE `residents_association`.`id` = 1096 ORDER BY `residents_association`.`name` ASC

I assume I've managed to create a circular reference, but if it were truly circular, it would just die, not run 4000x and then return. Having trouble finding a good Google or StackOverflow result for this.

© Stack Overflow or respective owner

Related posts about django

Related posts about admin