Allow customizing SQL expression of field

This commit is contained in:
Cédric Krier
2026-01-29 15:24:10 +01:00
parent dfe58aa125
commit 52326c4b08
4 changed files with 24 additions and 1 deletions
+1
View File
@@ -1,3 +1,4 @@
* Add support for column_<field name> method
* Use tables and Model as arguments for sql_column of the Field
* Add support for basic authentication for user application
* Support the conversion of MJML reports to HTML
+11
View File
@@ -222,6 +222,17 @@ Default value
See :ref:`default value <topics-fields_default_value>`
Reading
=======
A class method could be defined for each field which must return a SQL
expression as column instead of the default one.
The method signature is::
column_<field name>(tables)
Where ``tables`` is a nested dictionary, see :ref:`tables <ref-tables>`.
Searching
=========
+11
View File
@@ -206,6 +206,16 @@ def on_change_with_result(fieldname):
return decorator
def column_method(func):
@wraps(func)
def wrapper(self, tables, Model):
method = getattr(Model, f'column_{self.name}', None)
if method:
return method(tables)
return func(self, tables, Model)
return wrapper
def domain_method(func):
@wraps(func)
def wrapper(self, domain, tables, Model):
@@ -421,6 +431,7 @@ class Field(object):
def sql_cast(self, expression):
return Cast(expression, self.sql_type().base)
@column_method
def sql_column(self, tables, Model):
table, _ = tables[None]
return Column(table, self.name)
+1 -1
View File
@@ -655,7 +655,7 @@ class ModuleTestCase(_DBTestCase):
def test_methods(mname, model, attr):
for prefixes in [['default_'],
['on_change_', 'on_change_with_'],
['order_'], ['domain_'], ['autocomplete_']]:
['column_'], ['order_'], ['domain_'], ['autocomplete_']]:
if attr in {'on_change_with', 'on_change_notify'}:
continue
# TODO those method should be renamed