From bea1c4fdb6b8fc812daff0fe400a21bea808220b Mon Sep 17 00:00:00 2001 From: Tommy Beadle Date: Tue, 20 Aug 2024 14:10:50 -0400 Subject: [PATCH] Determine if a field is async or not by the presence of ato_representation. Don't hard-code the list of DRF_FIELDS--this was missing things like SerializerMethodField, ManyRelatedField, and ReadOnlyField. --- adrf/serializers.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/adrf/serializers.py b/adrf/serializers.py index 433925d..00b0a84 100644 --- a/adrf/serializers.py +++ b/adrf/serializers.py @@ -1,3 +1,4 @@ +import asyncio import traceback from collections import OrderedDict @@ -15,14 +16,6 @@ from rest_framework.serializers import Serializer as DRFSerializer from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList -# NOTE This is the list of fields defined by DRF for which we need to call to_rapresentation. -DRF_FIELDS = list(DRFModelSerializer.serializer_field_mapping.values()) + [ - DRFModelSerializer.serializer_related_field, - DRFModelSerializer.serializer_related_to_field, - DRFModelSerializer.serializer_url_field, - DRFModelSerializer.serializer_choice_field, -] - class BaseSerializer(DRFBaseSerializer): """ @@ -159,10 +152,10 @@ async def ato_representation(self, instance): if check_for_none is None: ret[field.field_name] = None else: - if type(field) in DRF_FIELDS: - repr = field.to_representation(attribute) - else: + if asyncio.iscoroutinefunction(getattr(field, "ato_representation", None)): repr = await field.ato_representation(attribute) + else: + repr = field.to_representation(attribute) ret[field.field_name] = repr