Allow customizing SQL expression of field
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
=========
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user