DRF Response field shows List of String with backslashes
I'm creating Polls feature using DRF where users can create a poll. The Response object in postman for the 'options' field shows the List of strings with backslashes.
Here is the Postman response :
{
"id": 17,
"question": "Who will win the Fifa World Cup 2026 in USA ?",
"options": "[\"Germany\",\"Argentina\",\"France\",\"Italy\",\"Brazil\",\"None of the above\"]",
}
My code which gives the above result :
//views.py//
class Polls(APIView):
permission_classes = (IsAuthenticated,)
def put(self, request):
request_data = request.data.dict()
serializer = PollPostRequestSerializer(data=request_data, context={"request": request})
serializer.is_valid(raise_exception=True)
data = serializer.validated_data
question = data.get('question')
options = data.get('options')
Poll = get_poll_model()
poll = Poll.create_poll(question=question, options=options)
poll_serialized = PollPostResponseSerializer(poll, many=False, context={"request": request}).data
return Response(poll_serialized, status=status.HTTP_200_OK)
//Request serializer//
class PollPostRequestSerializer(serializers.Serializer):
question = serializers.CharField(max_length=1000, required=True, allow_blank=True, allow_null=False)
options = serializers.CharField(max_length=1000, required=True, allow_blank=True, allow_null=False)
//Response serializer//
class PollPostResponseSerializer(serializers.ModelSerializer):
class Meta:
model = PollPost
fields = (
'id',
'question',
'options',
)
//models.py//
class PollPost(models.Model):
question = models.TextField(blank=False, null=False)
options = models.TextField(blank=False, null=False)
@classmethod
def create_poll(cls, question, options):
return cls.objects.create(question=question,options=options)
My purpose is to get the Response object without the backslashes. Help appreciated.
Here you need to Convert the list to a JSON string before saving and Need to Convert the JSON string back to a list when retrieving
# Update the PollPostRequestSerializer
from rest_framework import serializers
class PollPostRequestSerializer(serializers.Serializer):
question = serializers.CharField(max_length=1000, required=True, allow_blank=True, allow_null=False)
options = serializers.ListField(
child=serializers.CharField(max_length=200), required=True
)
# Update the PollPostResponseSerializer
class PollPostResponseSerializer(serializers.ModelSerializer):
options = serializers.ListField(child=serializers.CharField())
class Meta:
model = PollPost
fields = ('id', 'question', 'options')
# Update the PollPost Model
class PollPost(models.Model):
question = models.TextField(blank=False, null=False)
options = models.TextField(blank=False, null=False)
@classmethod
def create_poll(cls, question, options):
# Convert the list to a JSON string before saving
options_json = json.dumps(options)
return cls.objects.create(question=question, options=options_json)
def get_options(self):
# Convert the JSON string back to a list when retrieving
return json.loads(self.options)
# Modify the Response in the View
class Polls(APIView):
permission_classes = (IsAuthenticated,)
def put(self, request):
serializer = PollPostRequestSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
data = serializer.validated_data
question = data.get('question')
options = data.get('options')
Poll = get_poll_model()
poll = Poll.create_poll(question=question, options=options)
# Deserialize options before sending response
poll_serialized = PollPostResponseSerializer(poll, many=False, context={"request": request}).data
poll_serialized['options'] = json.loads(poll_serialized['options'])
return Response(poll_serialized, status=status.HTTP_200_OK)