Làm cách nào để tạo giả "chọn" thủ công để tránh các lần truy cập DB không mong muốn?Chọn liên quan đến Django trong yêu cầu thô
ta có:
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
Làm thế nào để nói với django rằng các mô hình liên quan đã lấy.
Bạn có thể thử sử dụng .values () để truy lục các thành phố dưới dạng từ điển. Xem: https: //docs.djangoproject.com/en/dev/ref/models/querysets/#values – Jingo