'none type object has no attribute 'replace'
I'm trying to replace None with 0 but I'm getting an error as none type object has no attribute 'replace'. This is what I have tried
views.py:
def Activity(UserID):
cursor = connection.cursor()
cursor.execute('EXEC [dbo].[sp_GetCurrentRunningActivityAudit] @UserId=%s',(UserID,))
result_set =cursor.fetchall()
data = []
for row in result_set:
TaskId = row[0]
data.append({
'TaskId':row[0],
'TaskName' : row[1],
'Source' : row[2],
'Requester' : row[3].replace('None', '0') if row[3] == None else row[3] ,
'type' : row[4],
'IsActive':GetCurrentSubTaskSTatus(TaskId),
})
print(data)
return Response(data[0], status=status.HTTP_200_OK)
Solution 1:[1]
row[3].replace('None', '0') if row[3] == None else row[3]
Reflect about it:
if row[3] == None:
row[3].replace('None', '0')
If row[3] is None, how can it be a string? And how can it contain 'None'?
You should replace that part with the following:
'0' if row[3] == None else row[3]
To better understand your problem, I would suggest you to read the following:
Solution 2:[2]
replace is a method for strings. None is not string, it is NoneType object.
You can try following code for your case:
'Requester' : row[3] or '0',
It will use value of row[3] if it is not None. Otherwise it will use '0'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | FLAK-ZOSO |
| Solution 2 | pL3b |
