def as_sql(self, bare_lookup, fallback=True):
'''
Compose the sql lookup to get the value for this virtual field in a query.
'''
language = self.get_language()
if language == DEFAULT_LANGUAGE:
return self._localized_lookup(language, bare_lookup)
if not fallback:
i18n_lookup = self._localized_lookup(language, bare_lookup)
return Cast(i18n_lookup, self.output_field())
fallback_chain = get_fallback_chain(language)
# first, add the current language to the list of lookups
lookups = [self._localized_lookup(language, bare_lookup)]
# and now, add the list of fallback languages to the lookup list
for fallback_language in fallback_chain:
lookups.append(
self._localized_lookup(fallback_language, bare_lookup)
)
return Coalesce(*lookups, output_field=self.output_field())
评论列表
文章目录