Python: Check if a String is an Integer

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 ]

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog