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.
if self.something.isdigit():
try:
id = int(self.something)
return ModelName.objects.get(id = id)
except ModelName.DoesNotExist:
return None;
[ Source ]