title: Django Query Performance: Unidirectional inclusion using ForeignKey vs. Bidirectional using ForeignKey & Related_name
body: <p>All, </p>
  
  <p>Which one do you prefer when it comes to proper design &amp; cleanness as well as query performance?</p>
  
  <p><strong>Option A:</strong> (bidirectional reference)</p>
  
  <pre><code>class Country(models.Model):
      name = models.CharField(max_length=128)
      capital = models.ForeignKey('City',related_name="country_capital", null=True)
  
  class State(models.Model):
      name = models.CharField(max_length=128)
      capital = models.ForeignKey('City', related_name="state_capital", null=True)
      country = models.ForeignKey('Country', null=True)
  
  class City(models.Model):
      name = models.CharField(max_length=128)
      state = models.ForeignKey('State', null=True)
      country = models.ForeignKey('Country', null=True)
  </code></pre>
  
  <p><strong>Option B:</strong> (unidirectional reference)</p>
  
  <pre><code>class Country(models.Model):
      name = models.CharField(max_length=128)
  
  class State(models.Model):
      name = models.CharField(max_length=128)
      country = models.ForeignKey('Country', null=True)
  
  class City(models.Model):
      name = models.CharField(max_length=128)
      state = models.ForeignKey('State', null=True)
      state_capital = models.BooleanField(default=False)
      country_capital = models.BooleanField(default=False)
  
  
  Search will be done as:
  Get me all cities where name is Waterloo and country is US.
  Get me all cities where country is UK.
  Get me all states where country is US.
  Get me a country where country is US. and capital is Washington DC.
  Get me the capital where country is US.
  </code></pre>