The issue is that I should be selecting the related models, and in my situation I should be selecting the related models with each query. So instead of doing select_related
in the views which I use the City
model, I made a custom model manager, like in this SO question: Django: Force select related?
So the City model is as follows now:
class WithState(models.Manager):
def get_query_set(self):
return super(WithState, self).get_query_set().select_related('state')
class City(models.Model):
name = models.CharField(max_length=100)
state = models.ForeignKey(State)
objects = WithState()
def __unicode__(self):
return u'(%s) %d. %s' % (self.state.name, self.id, self.name)
Load time is now down to 3 seconds.