Odoo - 添加自定义字段属性?

And*_*ius 9 python attributes field openerp odoo-8

有没有办法在Odoo中添加自定义字段属性?例如,每个字段都有属性help,您可以在其中输入消息来解释用户的字段.所以我想添加自定义属性,这样就会改变字段对所有类型字段的行为方式.

我想要添加到Field类中,因此所有字段都将获得该属性.但似乎无论我做什么,Odoo都没有看到这样的属性被添加.

如果我只是添加新的自定义属性,如:

some_field = fields.Char(custom_att="hello")
Run Code Online (Sandbox Code Playgroud)

然后它被忽略了.而且我需要通过方法获取它fields_get,它可以返回所需的属性值(信息它的作用:

def fields_get(self, cr, user, allfields=None, context=None, write_access=True, attributes=None):
    """ fields_get([fields][, attributes])

    Return the definition of each field.

    The returned value is a dictionary (indiced by field name) of
    dictionaries. The _inherits'd fields are included. The string, help,
    and selection (if present) attributes are translated.

    :param allfields: list of fields to document, all if empty or not provided
    :param attributes: list of description attributes to return for each field, all if empty or not provided
    """
Run Code Online (Sandbox Code Playgroud)

所以调用它,不会返回我的自定义属性(它确实返回最初由Odoo定义的属性).

我也尝试_slotsField课堂上更新(使用猴子补丁或仅通过更改源代码进行测试)属性,但似乎还不够.因为我的属性仍然被忽略.

from openerp import fields

original_slots = fields.Field._slots

_slots = original_slots
_slots['custom_att'] = None

fields.Field._slots = _slots
Run Code Online (Sandbox Code Playgroud)

有谁知道如何正确添加字段的新自定义属性?

Red*_*d15 5

假设 v9

结果fields_get是模型上定义的字段的摘要,代码显示只有在描述已填充的情况下才会添加属性。它将通过调用获取当前字段的描述field.get_description

因此,为了确保您的属性插入其中,self.description_attrs您将需要添加一个以_description_customatt(customatt示例中的部分) 开头的属性或方法,并将返回所需的数据。

我没有为此运行任何测试,但您可以查看字段的代码及其实际返回的属性。例如帮助属性描述(src

def _description_help(self, env):
  if self.help and env.lang:
    model_name = self.base_field.model_name
    field_help = env['ir.translation'].get_field_help(model_name)
    return field_help.get(self.name) or self.help
  return self.help
Run Code Online (Sandbox Code Playgroud)