diff options
Diffstat (limited to 'src/dashboard/utils.py')
-rw-r--r-- | src/dashboard/utils.py | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/src/dashboard/utils.py b/src/dashboard/utils.py index d6b697a..97c9ac7 100644 --- a/src/dashboard/utils.py +++ b/src/dashboard/utils.py @@ -7,7 +7,7 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from django.core.exceptions import ObjectDoesNotExist +from django.core.exceptions import (ObjectDoesNotExist, MultipleObjectsReturned) class AbstractModelQuery(): @@ -38,7 +38,15 @@ class AbstractModelQuery(): @classmethod def get(cls, *args, **kwargs): + """ + Gets a single matching resource + Throws ObjectDoesNotExist if none found matching, or MultipleObjectsReturned if + the query does not narrow to a single object + """ try: + ls = cls.filter(*args, **kwargs) + if len(ls) > 1: + raise MultipleObjectsReturned() return cls.filter(*args, **kwargs)[0] except IndexError: raise ObjectDoesNotExist() |