

def get_encounters(self, obj):
    version_objects = Version.objects.all()
    version_data = VersionSummarySerializer(version_objects, many=True, context=self.context).data
    all_encounters = Encounter.objects.filter(location_area=obj).order_by('pokemon')
    encounters_list = []
    for poke in all_encounters.values('pokemon').distinct():
        pokemon_object = Pokemon.objects.get(pk=poke['pokemon'])
        pokemon_detail = OrderedDict()
        pokemon_detail['pokemon'] = PokemonSummarySerializer(pokemon_object, context=self.context).data
        pokemon_detail['version_details'] = []
        poke_encounters = all_encounters.filter(pokemon=poke['pokemon']).order_by('version')
        for ver in poke_encounters.values('version').distinct():
            version_detail = OrderedDict()
            version_detail['version'] = version_data[(ver['version'] - 1)]
            version_detail['max_chance'] = 0
            version_detail['encounter_details'] = []
            poke_data = EncounterDetailSerializer(poke_encounters.filter(version=ver['version']), many=True, context=self.context).data
            for encounter in poke_data:
                slot = EncounterSlot.objects.get(pk=encounter['encounter_slot'])
                slot_data = EncounterSlotSerializer(slot, context=self.context).data
                del encounter['pokemon']
                del encounter['encounter_slot']
                del encounter['location_area']
                del encounter['version']
                encounter['chance'] = slot_data['chance']
                version_detail['max_chance'] += slot_data['chance']
                encounter['method'] = slot_data['encounter_method']
                version_detail['encounter_details'].append(encounter)
            pokemon_detail['version_details'].append(version_detail)
        encounters_list.append(pokemon_detail)
    return encounters_list

dataset/ETHPy150Open phalt/pokeapi/pokemon_v2/serializers.py LocationAreaDetailSerializer.get_encounters/original