Pre_save信号将值传递给django rest序列化程序

时间:2014-04-27 20:34:25

标签: django django-rest-framework

我有以下代码不起作用。这是我的错误:

  

/ make-call /

中的AttributeError      

'unicode'对象没有属性'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')

我只是想让它显示

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

0 个答案:

没有答案