MCPcopy
hub / github.com/PokeAPI/pokeapi / get_held_by_pokemon

Method get_held_by_pokemon

pokemon_v2/serializers.py:1944–1974  ·  view source on GitHub ↗
(self, obj)

Source from the content-addressed store, hash-verified

1942 }
1943 )
1944 def get_held_by_pokemon(self, obj):
1945 pokemon_items = PokemonItem.objects.filter(item=obj).order_by("pokemon_id")
1946 pokemon_ids = pokemon_items.values("pokemon_id").distinct()
1947 pokemon_list = []
1948
1949 for id in pokemon_ids:
1950 item_pokemon_details = OrderedDict()
1951
1952 # Get each Unique Item by ID
1953 pokemon_object = Pokemon.objects.get(pk=id["pokemon_id"])
1954 pokemon_data = PokemonSummarySerializer(
1955 pokemon_object, context=self.context
1956 ).data
1957 item_pokemon_details["pokemon"] = pokemon_data
1958
1959 # Get Versions associated with each unique item
1960 pokemon_item_objects = pokemon_items.filter(pokemon_id=id["pokemon_id"])
1961 serializer = PokemonItemSerializer(
1962 pokemon_item_objects, many=True, context=self.context
1963 )
1964 item_pokemon_details["version_details"] = []
1965
1966 for pokemon in serializer.data:
1967 version_detail = OrderedDict()
1968 version_detail["rarity"] = pokemon["rarity"]
1969 version_detail["version"] = pokemon["version"]
1970 item_pokemon_details["version_details"].append(version_detail)
1971
1972 pokemon_list.append(item_pokemon_details)
1973
1974 return pokemon_list
1975
1976 @extend_schema_field(
1977 field={

Callers

nothing calls this directly

Calls 3

getMethod · 0.80

Tested by

no test coverage detected