Pre_save signal to pass a value to a django rest s

2019-09-17 10:53发布

问题:

I have the following code that does not work. This is my Error:

AttributeError at /make-call/

'unicode' object has no attribute 'opentok_sessionid'

Views.py

@api_view(['POST'])
def startCall(request):

if request.method == 'POST':

    API_KEY = '111111'
    API_SECRET = '11111111111'
    OTSDK = OpenTokSDK.OpenTokSDK(API_KEY, API_SECRET)
    opentok_session = str(OTSDK.create_session().session_id)

    serializer = startCallSerializer(data=request.DATA)

    if serializer.is_valid():

        def pre_save(self, obj):
            obj.opentok_sessionid = opentok_session

        serializer.save()

        return Response(serializer.data, status=status.HTTP_201_CREATED)

    else:

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

serializerrs.py

class startCallSerializer(serializers.ModelSerializer):

opentok_sessionid = serializers.Field(source='opentok_sessionid.opentok_sessionid')

class Meta:
      model = call
      fields = ('tutor', 'billed', 'opentok_sessionid')

I just want it to show

[ 
  { 
     "opentok_sessionid":"32323hj32h4j23h4kj234"
  }
]