Test if count is callable before using it

ModelStorage.count has been added in 6.2 but there are existing Model that has
field with this name.

Closes #12016
This commit is contained in:
Cédric Krier
2023-01-17 22:54:01 +01:00
parent d0dea0efca
commit 0b67a61607
3 changed files with 7 additions and 4 deletions
+1 -1
View File
@@ -259,7 +259,7 @@ class Many2One(Field):
else:
_, target_name = name.split('.', 1)
target_domain = [(target_name,) + tuple(domain[1:])]
if Target.count() < _subquery_threshold:
if callable(Target.count) and Target.count() < _subquery_threshold:
query = Target.search(target_domain, order=[], query=True)
return column.in_(query)
else:
+2 -1
View File
@@ -337,7 +337,8 @@ class One2Many(Field):
origin_where = origin.like(Model.__name__ + ',%')
origin = origin_field.sql_id(origin, Target)
use_in = Target.count() < _subquery_threshold
use_in = (
callable(Target.count) and Target.count() < _subquery_threshold)
if '.' not in name:
if value is None:
if use_in:
+4 -2
View File
@@ -1647,7 +1647,9 @@ class ModelSQL(ModelStorage):
main_table, _ = tables[None]
if count:
table = convert_from(None, tables)
if (limit is not None and limit < cls.count()) or offset:
if (limit is not None
and callable(cls.count)
and limit < cls.count()) or offset:
select = table.select(
Literal(1), where=expression, limit=limit, offset=offset
).select(Count(Literal('*')))
@@ -1878,7 +1880,7 @@ class ModelSQL(ModelStorage):
'You can not update fields: "%s", "%s"' %
(field.left, field.right))
if len(ids) < max(cls.count() / 4, 4):
if callable(cls.count) and len(ids) < max(cls.count() / 4, 4):
for id_ in ids:
cls._update_tree(id_, field_name,
field.left, field.right)