A handy way of checking if a string can be converted into an integer is to use isdigit(), which will will return a boolean value.
1.
if
self
.something.isdigit():
2.
try
:
3.
id
=
int(
self
.something)
4.
return
ModelName.objects.get(id
=
id)
5.
except
ModelName.DoesNotExist:
6.
return
None
;
[ Source ]