diff --git a/api/api/views/media_views.py b/api/api/views/media_views.py index d003feec9fd..906e5974700 100644 --- a/api/api/views/media_views.py +++ b/api/api/views/media_views.py @@ -105,6 +105,18 @@ def _get_request_serializer(self, request): return req_serializer def get_db_results(self, results): + """ + Map ES hits to ORM model instances. + + ORM instances have all necessary info needed for serializers whereas ES + hits only contain the subset of fields needed for indexing and search. + This function issues one query to the DB, using the ``identifier`` field + which is both unique and indexed, so it's quite performant. + + :param results: the list of ES hits + :return: the corresponding list of ORM model instances + """ + identifiers = [] hits = [] for hit in results: @@ -267,6 +279,10 @@ def related(self, request, identifier=None, *_, **__): serializer_context = self.get_serializer_context() + serializer_class = self.get_serializer() + if serializer_class.needs_db: + results = self.get_db_results(results) + serializer = self.get_serializer(results, many=True, context=serializer_context) return self.get_paginated_response(serializer.data) diff --git a/api/test/media_integration.py b/api/test/media_integration.py index f77ae5af0eb..0a4563344ae 100644 --- a/api/test/media_integration.py +++ b/api/test/media_integration.py @@ -227,6 +227,10 @@ def get_terms_set(res): or result["creator"] == item["creator"] ), f"{terms_set} {get_terms_set(result)}/{result['creator']}-{item['creator']}" + assert result["license_version"] is not None + assert result["attribution"] is not None + assert result["creator_url"] is not None + def sensitive_search_and_detail(media_type): search_res = requests.get(