def cache_translations(objects, languages=None, meta=None): """ Cache translation objects in given languages to the objects in one fell swoop. This will iterate a queryset, if one is passed! :param objects: List or queryset of Translatable models :param languages: Iterable of languages to fetch. In addition, all "_current_language"s will be fetched :return: objects """ if not objects: return objects languages = set(languages or ()) if meta is None: meta = objects[0]._parler_meta.root # work on base model by default xlate_model = meta.model object_map = dict((object.pk, object) for object in objects) languages.update(set(object._current_language for object in objects)) master_ids = object_map.keys() # SQLite limits host variables to 999 (see http://www.sqlite.org/limits.html#max_variable_number), # so we're batching to a number around that, with enough leeway for other binds (`languages` in particular). for master_ids in batch(master_ids, 950): for translation in xlate_model.objects.filter( master_id__in=master_ids, language_code__in=languages): master = object_map[translation.master_id] master._translations_cache[xlate_model][ translation.language_code] = translation # FIXME: setattr(translation, translation.__class__.master.cache_name, master) return objects
def cache_translations(objects, languages=None, meta=None): """ Cache translation objects in given languages to the objects in one fell swoop. This will iterate a queryset, if one is passed! :param objects: List or queryset of Translatable models :param languages: Iterable of languages to fetch. In addition, all "_current_language"s will be fetched :return: objects """ if not objects: return objects languages = set(languages or ()) if meta is None: meta = objects[0]._parler_meta.root # work on base model by default xlate_model = meta.model object_map = dict((object.pk, object) for object in objects) languages.update(set(object._current_language for object in objects)) master_ids = object_map.keys() # SQLite limits host variables to 999 (see http://www.sqlite.org/limits.html#max_variable_number), # so we're batching to a number around that, with enough leeway for other binds (`languages` in particular). for master_ids in batch(master_ids, 950): for translation in xlate_model.objects.filter(master_id__in=master_ids, language_code__in=languages): master = object_map[translation.master_id] master._translations_cache[xlate_model][translation.language_code] = translation setattr(translation, translation.__class__.master.cache_name, master) return objects