Python: Check if a String is an Integer

0 Comments

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 ]

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