[IMP] models: faster ensure_one implementation

Calling `len` on a long list and checking that its value is 1 is slower than
just unpacking the list.  The point is to check whether there is just one item
in the list, and counting all items just for that is a bit overkill.
This commit is contained in:
Denis Ledoux
2019-02-11 15:50:51 +00:00
committed by Raphael Collet
parent dcc752aaab
commit ce0575ef27
+5 -2
View File
@@ -4367,9 +4367,12 @@ class BaseModel(MetaModel('DummyModel', (object,), {'_register': False})):
""" Verifies that the current recorset holds a single record. Raises
an exception otherwise.
"""
if len(self) == 1:
try:
# unpack to ensure there is only one value is faster than len for big lists
_id, = self._ids
return self
raise ValueError("Expected singleton: %s" % self)
except ValueError:
raise ValueError("Expected singleton: %s" % self)
def with_env(self, env):
""" Returns a new version of this recordset attached to the provided