2016-06-10 421 views
9

有沒有辦法在Odoo中添加自定義字段屬性?例如,每個字段都有屬性help,您可以在其中輸入解釋用戶字段的消息。所以我想添加自定義屬性,這樣就可以改變字段對所有類型字段的作用方式。Odoo - 添加自定義字段屬性?

我想添加到Field類中,所以所有字段都會獲得該屬性。但看起來不管我做什麼,Odoo都沒有看到這個屬性被添加了。

如果我簡單地添加新的自定義屬性,如:

some_field = fields.Char(custom_att="hello") 

然後,它會被忽略。而我需要它的方法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 
    """ 

所以調用它,不回我的自定義屬性(它返回原先定義的值Odoo雖然)。

我也嘗試更新_slots(與猴子補丁或只是在Field類改變源代碼)屬性測試,但現在看來,這是不夠的。因爲我的屬性是被忽略。

from openerp import fields 

original_slots = fields.Field._slots 

_slots = original_slots 
_slots['custom_att'] = None 

fields.Field._slots = _slots 

有誰知道如何正確地爲字段添加新的自定義屬性?

回答

3

假設V9

fields_get其結果是在一個模型中定義字段的摘要,the code表明,它只會增加該屬性如果描述填充。它將因此,爲了獲取當前場by calling 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 
+1

我想這和它的作品,雖然你還需要加上'_slots'字典裏面那個屬性(在'Field'類),因此它會得到默認值,否則Odoo會提出錯誤。現在我需要弄清楚如何在不直接修改源代碼的情況下應用它。 – Andrius

0

如果您在自己的服務器上運行OpenERP/ODOO(換句話說,不是您無法訪問的代碼的雲版本),您只能這樣做。

您將需要修改<base>/osv/fields.py文件,對文件(基_column類已經節省了額外的關鍵字參數給你 - 至少在7.0版)的底部添加您更改field_to_dict功能:

def field_to_dict(model, cr, user, field, context=None): 
    res = {'type': field._type} 
    ... 
    ... 
    for arg in ('string', 'readonly', ...) : 

某處在那麼長的屬性列表,你需要插入一個你感興趣的名稱。

或者,你可以更新_column.__init__節省的額外參數的名稱,field_to_dict包括日EM(未測試):

diff -r a30d30db3cd9 osv/fields.py 
--- a/osv/fields.py Thu Jun 09 17:18:29 2016 -0700 
+++ b/osv/fields.py Mon Jun 13 18:11:26 2016 -0700 
@@ -116,23 +116,24 @@ class _column(object): 
     self._context = context 
     self.write = False 
     self.read = False 
     self.view_load = 0 
     self.select = select 
     self.manual = manual 
     self.selectable = True 
     self.group_operator = args.get('group_operator', False) 
     self.groups = False # CSV list of ext IDs of groups that can access this field 
     self.deprecated = False # Optional deprecation warning 
-  for a in args: 
-   if args[a]: 
-    setattr(self, a, args[a]) 
+  self._user_args =() 
+  for name, value in args: 
+   setattr(self, name, value or False) 
+   self._user_args += name 

    def restart(self): 
     pass 

    def set(self, cr, obj, id, name, value, user=None, context=None): 
     cr.execute('update '+obj._table+' set '+name+'='+self._symbol_set[0]+' where id=%s', (self._symbol_set[1](value), id)) 

    def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None): 
     raise Exception(_('undefined get method !')) 

@@ -1559,20 +1560,22 @@ def field_to_dict(model, cr, user, field 
     res['o2m_order'] = field._order or False 
    if isinstance(field, many2many): 
     (table, col1, col2) = field._sql_names(model) 
     res['m2m_join_columns'] = [col1, col2] 
     res['m2m_join_table'] = table 
    for arg in ('string', 'readonly', 'states', 'size', 'group_operator', 'required', 
      'change_default', 'translate', 'help', 'select', 'selectable', 'groups', 
      'deprecated', 'digits', 'invisible', 'filters'): 
     if getattr(field, arg, None): 
      res[arg] = getattr(field, arg) 
+ for arg in field._user_args: 
+  res[arg] = getattr(field, arg) 

    if hasattr(field, 'selection'): 
     if isinstance(field.selection, (tuple, list)): 
      res['selection'] = field.selection 
     else: 
      # call the 'dynamic selection' function 
      res['selection'] = field.selection(model, cr, user, context) 
    if res['type'] in ('one2many', 'many2many', 'many2one'): 
     res['relation'] = field._obj 
     res['domain'] = field._domain(model) if callable(field._domain) else field._domain