Django Tweak The Rest Framework Serializer To Getting A Different Json Structure
I have a django app using rest framework in which i am serializing a model in my serializers.py as follows: class CourseSerializer(serializers.ModelSerializer): class Meta:
Solution 1:
You will probably have to implement a wrapper for your CourseSerializer
. Try the code below.
classCourseSerializer(serializers.ModelSerializer):
class Meta:
model = Course
depth = 1classCourseWrapperSerializer(serializers.Serializer):
course = CourseSerializer(read_only=True, source='*')
The crucial thing here is source='*
indicating that the entire object should be passed through to the field. Then in your view use the wrapper instead of the original serializer.
classCourseSubjectList(APIView):
defget(self, request, pk, format=None):
subs = Course.objects.all()
serialized_subs = CourseWrapperSerializer(subs, many=True)
return Response(serialized_subs.data)
Post a Comment for "Django Tweak The Rest Framework Serializer To Getting A Different Json Structure"