]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Added Query.scalar() sugar method, eases migration from old query.sum() methods....
authorJason Kirtland <jek@discorporate.us>
Thu, 11 Sep 2008 17:52:35 +0000 (17:52 +0000)
committerJason Kirtland <jek@discorporate.us>
Thu, 11 Sep 2008 17:52:35 +0000 (17:52 +0000)
lib/sqlalchemy/orm/query.py

index 5bb42df2f30f7fe578f53181901f8120d2ef29a7..cb1773e5ae6d289209ae7d6111da5c92fdd4c94c 100644 (file)
@@ -1037,6 +1037,17 @@ class Query(object):
             raise orm_exc.MultipleResultsFound(
                 "Multiple rows were found for one()")
 
+    def scalar(self):
+        """Return """
+
+        ret = list(self)[0]
+        if not isinstance(ret, tuple):
+            return ret
+        try:
+            return ret[0]
+        except IndexError:
+            return None
+
     def __iter__(self):
         context = self._compile_context()
         context.statement.use_labels = True