2016-06-10 3 views
9

Odoo에서 사용자 정의 필드 속성을 추가하는 방법이 있습니까? 예를 들어 모든 필드에는 사용자의 필드를 설명하는 메시지를 입력 할 수있는 help 속성이 있습니다. 그래서 사용자 정의 속성을 추가하여 필드의 모든 유형에 대해 필드가 작동하는 방식을 변경합니다.Odoo - 사용자 정의 필드 속성을 추가 하시겠습니까?

Field 클래스에 추가하고 싶습니다. 모든 필드에서 해당 속성을 가져옵니다. 하지만 내가하는 일과 상관없이 Odoo는 그런 속성이 추가 된 것을 보지 못합니다.

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

그런 다음이 단순히 무시된다 :

단순히 새로운 사용자를 추가하는 경우

좋아 때문이다.

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 
    """ 

그래서 전화, 내 사용자 지정 특성을 반환하지 않습니다 (원래에 의해 정의 된 사람을 반환하지 : 그리고 나는 그것이 원하는 속성 값 (정보가 무엇을 반환 할 수 있습니다 방법 fields_get,에 의해 선택 될 필요가 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는 설명가 작성되었을 경우에만 속성을 추가 할 것을 보여줍니다. 그것은 당신의 속성은 당신이 (당신의 예에서 customatt 부분) _description_customatt로 시작하고 반환하는 속성 또는 메서드를 추가해야합니다이 self.description_attrs에 삽입됩니다 수 있도록 그래서 위해 현재 필드 by calling field.get_description

의 설명을 가져옵니다 필요한 데이터.

나는이 테스트를 실행하지 않았지만 실제로 리턴하는 필드와 속성의 코드를 볼 수 있습니다.예를 들어, 도움말 특성 설명 (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

나는이 시도하고 당신은 또한 ('Field' 클래스)'_slots' 사전 안에 그 속성을 추가해야하지만 그것은 작동, 그래서 기본 값을 얻을 것이다 그렇지 않으면 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