如何使「手動」select_related模仿,以避免不必要的數據庫命中?Django選擇在原始請求相關
我們:
class Country:
name = CharField()
class City:
country = models.ForeignKey(Country)
name = models.CharField()
cities = City.objects.raw("select * from city inner join country on city.country_id = country.id where name = 'london'")
#this will hill hit DB
print cities[0].country.name
如何告訴Django是相關的模型已經取出。
你可以嘗試使用.values()來檢索城市作爲字典。請參閱:https://docs.djangoproject.com/en/dev/ref/models/querysets/#values – Jingo 2012-02-17 12:42:48