0

I am new in python. How can call function from another function. Here is an example :

class TSMTViewSet(viewsets.ModelViewSet):
    queryset = Tsmt.objects.all()
    serializer_class = TsmtSerializer

def RunNew2N3(fromempid,toempid,fromdate,todate,fromcscid='',tocscid=''):
    cursor = connection.cursor()
    try:
        cursor.callproc('[dbo].[SPRUN]',[fromempid,toempid,fromdate,todate,fromcscid,tocscid])        
    finally:
        cursor.close()

def create(self,request):
    tsmt  = request.data.get('tsmt')

    tsmt['createby']="Admin"             
    tsmt['createdate']=datetime.datetime.now()    

    fromempid = tsmt['empid']
    toempid = tsmt['empid']
    fromdate = tsmt['tdate']
    todate = tsmt['tdate']




    serializer = TsmtSerializer(data=tsmt)

    if serializer.is_valid():
        tsmt_saved = serializer.save()

        RunNew2N3(fromempid,toempid,fromdate,todate)

        return Response({"result":"TSMT '{}' created successfully".format(tsmt_saved.tdate),"status":"success"})
    else:
        return Response({"result":"'{}'".format(serializer.errors),"status":"fail"})

I put the call function on top and why I can't call that.I got the error message

NameError: name 'RunNew2N3' is not defined

4
  • Are these methods or functions? Are they in a class? create takes a self argument but RunNew2N3 does not. Please show the full code and how you're calling it. Commented Sep 5, 2019 at 10:16
  • @DanielRoseman , Please see the update. Commented Sep 5, 2019 at 10:20
  • That hasn't really helped. Is the indentation correct? Are RunNew2N3 and create inside TSMTViewSet? If not, why does create take self? If so, please fix the indentation and explain why RunNew2N3 does not take self. Commented Sep 5, 2019 at 10:23
  • Indentation is correct and both functions are inside the views. I know indenting in question is incorrect but I don't know how to use the tag in here. create take self because of this is request method and I don't know whether should I put also self in RunNew2N3 function. I am a new dude. Commented Sep 5, 2019 at 10:28

2 Answers 2

1

This looks like an indentation issue, if RunNew2N3 isn't part of TSMTViewSet, define it outside and also align create inside the class:

def RunNew2N3(fromempid,toempid,fromdate,todate,fromcscid='',tocscid=''):
    cursor = connection.cursor()
    try:
        cursor.callproc('[dbo].[SPRUN]',[fromempid,toempid,fromdate,todate,fromcscid,tocscid])        
    finally:
        cursor.close()

class TSMTViewSet(viewsets.ModelViewSet):
    queryset = Tsmt.objects.all()
    serializer_class = TsmtSerializer

    def create(self,request):
        tsmt  = request.data.get('tsmt')

        tsmt['createby']="Admin"             
        tsmt['createdate']=datetime.datetime.now()    

        fromempid = tsmt['empid']
        toempid = tsmt['empid']
        fromdate = tsmt['tdate']
        todate = tsmt['tdate']

        serializer = TsmtSerializer(data=tsmt)

        if serializer.is_valid():
            tsmt_saved = serializer.save()

            RunNew2N3(fromempid,toempid,fromdate,todate)

            return Response({"result":"TSMT '{}' created successfully".format(tsmt_saved.tdate),"status":"success"})
        else:
            return Response({"result":"'{}'".format(serializer.errors),"status":"fail"})

Sign up to request clarification or add additional context in comments.

1 Comment

Indentation is exactly same with u , Sry I try to do in question and I can't use tab in question. If Runnew2n3 is part of create, how to write this code?
0

This is basic Python. If a function is a method on a class instance, it needs to:

  • be indented within the class
  • accept self as the first parameter
  • be called via self.

So:

class TSMTViewSet(viewsets.ModelViewSet):
    ...

    def RunNew2N3(self, fromempid, toempid, fromdate, todate, fromcscid='', tocscid=''):
        ...

    def create(self, request):
        ...
        self.RunNew2N3(fromempid, toempid, fromdate, todate)
        ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.