Minor feature request: Add a way to specify how Serializer.to_repr
handles `None
#7811
Replies: 3 comments 3 replies
-
Just following up that I am willing to write this should the team want to add this functionaility to DRF. |
Beta Was this translation helpful? Give feedback.
-
I just run into this issue when I wanted a nullable boolean field to represent None as False: The serializers.BooleanField's to_representation is:
It's clearly supposed to return False when
|
Beta Was this translation helpful? Give feedback.
-
I've just run exactly into the same issue with the |
Beta Was this translation helpful? Give feedback.
-
Currently all None values are sent to the API as None. It would be preferable to tell drf what to use for "None". e.g. our UI currently expects an empty list for a field if it's none because we were using a SerializerMethodField. We switched to a custom field to allow for read-write, and now DRF returns None even though we need an empty list.
I propose adding a way to bypass this
if
and always callto_representation
regardless ofattribute
, or add a way to specify what a field's"None"
is. I am open to writing this myself if the team wants it, but doesn't want to write it.Could maybe be changed to something like this
Checklist
master
branch of Django REST framework.Beta Was this translation helpful? Give feedback.
All reactions