我有一个多语言字段,我使用了 hvad 包。 我有一个如下所示的脚本,用于美味馅饼脱水。
array = []
for t in bundle.obj.facilities.filter(foo_type = i.foo_type):
for field in get_translatable_fields(t.foo_type.__class__):
for translation in t.foo_type.translations.all():
value = getattr(translation, field)
array.append(value)
print array
但我在同一个列表中得到了所有语言翻译。您是否知道让不同的列表属于不同的语言。
我只是想让不同的数组在翻译过程中区分......
迭代
最佳答案
您可以使用 collections.defaultdict
将它们存储在字典中,由 translation
索引:
import collections
dict_all = collections.defaultdict(list)
for t in bundle.obj.facilities.filter(foo_type = i.foo_type):
for field in get_translatable_fields(t.foo_type.__class__):
for translation in t.foo_type.translations.all():
value = getattr(translation, field)
dict_all[translation.language_code].append(value)
如果您想稍后将其恢复为常规字典(而不是 defaultdict
):
dict_all = dict(dict_all.items())
关于python - 根据翻译区分列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15992225/