From 98bb029ce56dcc5dc75293d661a00cbd67860b63 Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Mon, 15 Jul 2024 18:12:01 -0500 Subject: [PATCH] feat: change candidate representation from id to username --- eox_nelp/pearson_vue/api/v1/serializers.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/eox_nelp/pearson_vue/api/v1/serializers.py b/eox_nelp/pearson_vue/api/v1/serializers.py index cb21c80d..b21a9c87 100644 --- a/eox_nelp/pearson_vue/api/v1/serializers.py +++ b/eox_nelp/pearson_vue/api/v1/serializers.py @@ -64,3 +64,23 @@ def get_course(self, obj): str or None: The string representation of the course if it exists, otherwise `None`. """ return str(obj.course) if obj.course else None + + def to_representation(self, instance): + """ + Convert the object instance to its dictionary representation. + + This method overrides the default `to_representation` method to include + the candidate's username in the serialized representation. + + Args: + instance (object): The object instance being serialized. + + Returns: + dict: A dictionary representation of the object instance, including + the candidate's username if a candidate is associated with the instance. + If no candidate is associated, the value will be None. + """ + representation = super().to_representation(instance) + representation["candidate"] = instance.candidate.username if instance.candidate else None + + return representation