[Fixed]-Django Rest Framework get key from serializer.data

1👍

The “id” key is in the “abs” dict, so you need access “abs” first. Then access “id” key.

Try this:

def create(self, request, *args, **kwargs):
    #data = json.dumps(request.data)
    user = self.request.user
    if user.groups.filter(name='advertisers').exists():
        serializer = CampaignSerializer(data=request.data)
        adsImageSerializer = AdsImagesSerializer()
        if serializer.is_valid():
            serializer.save(advertiser=self.request.user)
            raise Exception(serializer.validated_data['abs']['id'])
            return Response(serializer.data, status=status.HTTP_201_CREATED,)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response({
                            'status':'Unauthorized',
                            'message':'This account is not authorized to do this request'
                    }, status=status.HTTP_401_UNAUTHORIZED)
    return super(CampaignStart, self).perform_create(serializer)

Leave a comment