Skip to content

Commit

Permalink
Determine if a field is async or not by the presence of ato_represent…
Browse files Browse the repository at this point in the history
…ation.

Don't hard-code the list of DRF_FIELDS--this was missing things like
SerializerMethodField, ManyRelatedField, and ReadOnlyField.
  • Loading branch information
tbeadle committed Aug 21, 2024
1 parent 226e2f0 commit bea1c4f
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions adrf/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import traceback
from collections import OrderedDict

Expand All @@ -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):
"""
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit bea1c4f

Please sign in to comment.