我有三个模型:Trip
,City
和GoogleApiCityCache
。
如果我想知道这个城市的名称,我会在@property
对象上使用City
,该对象试图获取当前语言的GoogleApiCityCache
。
我想减少模板中的一些查询:
{% for trip in trips %}
{{ trip.city.formatted_address }}
{% endfor %}
现在,它执行新的数据库查询以在每次迭代中查找GoogleApiCityCache
。我想预取这些缓存。
因此,属性formatted_address
必须从此对象中找到GoogleApiCityCache
并获取formatted_address
。
尝试使用prefetch_related
,但它没有帮助。
def profile(request, slug):
owner = User.objects.filter(userprofile__slug=slug).prefetch_related('trips__city__city_caches').first()
trips = Trip.objects.filter(user=owner).select_related('city').prefetch_related('city__city_caches')
return render(request, 'profiles/profile/profile.html', {'owner': owner,'trips':trips})
我的模特:
class Trip(models.Model):
user = models.ForeignKey('auth.User', related_name='trips')
city = models.ForeignKey('locations.City', related_name='trips')
date_from = models.DateField()
date_to = models.DateField()
detail = models.TextField(null=True, blank=True)
# participants = models.ManyToManyField('auth.User', related_name='participating_on_trips', blank=True)
objects = TripManager()
class City(models.Model):
...
def get_cache_by_lang(self, lang=None, force_refresh=False):
if not lang:
lang = get_language() or 'en'
cache, requested = GoogleApiCityCache.objects.get_or_request(city=self, lang=lang)
return cache
@property
def formatted_address(self):
return self.get_cache_by_lang().formatted_address
class GoogleApiCityCacheManager(models.Manager):
def _request_create(self, city, lang):
""" Create cache (wasn't found) """
....
return cache
def get_or_request(self, city, lang):
created = False
try:
cache = GoogleApiCityCache.objects.get(city=city, language_code=lang)
except GoogleApiCityCache.DoesNotExist:
cache = self._request_create(city, lang)
created = True
return cache,created
class GoogleApiCityCache(models.Model):
language_code = models.CharField(max_length=5)
api_json_response = JSONField(null=True, blank=True)
city = models.ForeignKey('locations.City', on_delete=models.CASCADE, related_name='city_caches')
objects = GoogleApiCityCacheManager()
class Meta:
unique_together = ('city', 'language_code')
@property
def formatted_address(self):
try:
return self.api_json_response['result']['formatted_address']
except:
return None
如您所见,GoogleApiCityCache
和city
的{{1}}可以是无。在这种情况下,请求Google Api,创建一个新的language_code
对象并返回。
答案 0 :(得分:1)
当您prefetch_related('trips__city__city_caches')
时,您正在预取city_caches
。这意味着以下循环不会导致任何额外的查询。
for cache in city.city_caches.all()
print(cache)
但是,在您的代码formatted_address
调用get_cache_by_lang
调用调用get_or_request
的{{1}},这将导致额外的查询。
为了利用GoogleApiCityCache.objects.get()
,您需要重写prefetch_related
,以便循环遍历get_cache_by_lang
以找到正确的对象。