2016-08-25 4 views
2

나는어떻게 함수 내에서 함수를 호출하는? Odoo9 검증

class product_pricelist_item(models.Model): 
    _inherit = 'product.pricelist.item' 

    myfield = fields.Boolean(string="CheckMark") 

product.pricelist.item에서 부울 필드를 추가 한 지금 product.pricelist.item에 여러 줄이 있습니다.

(확인) 나는 사용자가 한 번에 True 수 있습니다 True 다수에게 myfield 하나 하나 개의 필드를 만들 수 없습니다 것을 원한다.

product.pricelist.item에서 카운터를주고 myfields의 숫자 인 True을 전달하여이 작업을 시도했습니다.

하지만이 오류는 저에게 주어집니다.

global name '_get_counter' is not defined

def _get_counter(self): 
    for r in self: 
     p=[] 
     p= r.env['product.pricelist.item'].search_read([('myfield', '=', True)], ['myfield']) 
     counter = len(p) 
    return counter 

@api.constrains('myfield') 
def _check_myfield(self): 
    counter = _get_counter(self) 
    for r in self: 
     if counter > 1: 
      raise exceptions.ValidationError("Multiple myfield In a PriceList of a Product is not allowed.") 

이제 두 번째 질문은 : -

당신이 가격표 항목을 만든 다음이 데이터베이스의 데이터를 반영하지 않습니다 가격표에 저장을 클릭

. pricelist를 클릭하면 데이터가 반영되어 저장됩니다 ... 왜 이렇게됩니까?

답변

3

self으로는 현재 클래스의 메소드를 호출 할 수 있습니다. 다음 코드

시도 :

바꾸기 코드

counter = _get_counter(self) 

counter = self._get_counter() 
+0

오류 : -'_get_counter()는 정확히 1 인수 (주어진 2)' – maharshi

+0

'counter = self._get_counter()'잘 작동합니다. – maharshi

1

_get_counter의 루프는 결과에 영향을주지 않고 할 수 있도록 검색 기록에 의존하지 않았다 사용 :

def _get_counter(self): 

    pricelist_obj = self.env['product.pricelist.item'] 
    counter = len(pricelist_obj.search_read([('myfield', '=', True)], ['myfield'])) 
    return counter 

@api.constrains('myfield') 
def _check_myfield(self): 

    counter = self._get_counter() 
    if counter > 1: 
     raise exceptions.ValidationError("Multiple myfield In a PriceList of a Product is not allowed.") 
관련 문제