Home > Back-end >  cannot convert null to strftime in django
cannot convert null to strftime in django

Time:03-30

I'm trying to convert the time in 12 hours format where I have few rows with time as NULL. I'm passing it as dictionary. How could I leave that NULL rows and convert others rows with have time

Here, what I have tried

views.py

def GetUserInProgress(userid):
    
        cursor = connection.cursor()
        cursor.execute('EXEC [dbo].[sp_GetUserInProgress] @UserId=%s', (userid,))
        result_set = cursor.fetchall()
        data =[]
        for i in range(len(result_set)):

              data.append({
                'TaskId':result_set[i][0],
                'CreatedOn':result_set[i][13],
                'CreatedBy':result_set[i][14],
                'StartedOn':result_set[i][15],
                'ClosedOn':result_set[i][16],
                'Requester':result_set[i][17],
                'StartedOnPST':result_set[i][31],
                'ClosedOnPST':result_set[i][32],
                'ClosedonPST_Final':result_set[i][33].strftime('%d-%m-%Y %I:%M  %p'), 

            })
              
        return Response(data)

CodePudding user response:

'ClosedonPST_Final':result_set[i][33].strftime('%d-%m-%Y %I:%M  %p') if result_set[i][33] else None

you can do a check

CodePudding user response:

Not tested, but it should work:

def GetUserInProgress(userid):
    def _emptyIfNull(v):
        return v.strftime('%d-%m-%Y %I:%M  %p') if v else ""

    cursor = connection.cursor()
    cursor.execute('EXEC [dbo].[sp_GetUserInProgress] @UserId=%s', (userid,))
    return Response([{
        'TaskId':result_set[i][0],
        'CreatedOn': _emptyIfNull(result_set[i][13]),
        'CreatedBy': _emptyIfNull(result_set[i][14]),
        'StartedOn': _emptyIfNull(result_set[i][15]),
        'ClosedOn': _emptyIfNull(result_set[i][16]),
        'Requester': _emptyIfNull(result_set[i][17]),
        'StartedOnPST': _emptyIfNull(result_set[i][31]),
        'ClosedOnPST': _emptyIfNull(result_set[i][32]),
        'ClosedonPST_Final': _emptyIfNullresult_set[i][33],
    } for i in cursor.fetchall()])
  • Related