

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(pokemon=obj).order_by('location_area')
    encounters_list = []
    for area in all_encounters.values('location_area').distinct():
        location_area_object = LocationArea.objects.get(pk=area['location_area'])
        location_area_detail = OrderedDict()
        location_area_detail['location_area'] = LocationAreaSummarySerializer(location_area_object, context=self.context).data
        location_area_detail['version_details'] = []
        area_encounters = all_encounters.filter(location_area=area['location_area']).order_by('version')
        for ver in area_encounters.values('version').distinct():
            version_detail = OrderedDict()
            version_detail['max_chance'] = 0
            version_detail['encounter_details'] = []
            version_detail['version'] = version_data[(ver['version'] - 1)]
            area_data = EncounterDetailSerializer(area_encounters.filter(version=ver['version']), many=True, context=self.context).data
            for encounter in area_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)
            location_area_detail['version_details'].append(version_detail)
        encounters_list.append(location_area_detail)
    return encounters_list

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