2016-11-15 3 views
1

이 내 구조 :Odoo 10의 객체에서 ID를 가져 오는 방법은 무엇입니까?

class Imprint_Location(models.Model): 
    _name = 'imprint.location' 

    name = fields.Char() 
    product_id = fields.Many2one('product.template') 

class Imprint_Charges(models.Model): 
    _name = 'imprint.charge' 
    _rec_name = 'location_id' 

    product_id_c = fields.Many2one('product.template', required=True) 
    location_id = fields.Many2one('imprint.location', required=True) 
    @api.multi 
    @api.onchange('product_id_c', 'location_id') 
    def product_filter(self): 
     res = {} 
     print '\n\n-------\n\n', self, self.product_id_c, '\n\n-------\n\n' 
     if self.product_id_c: 
      res['domain'] = {'location_id': [('product_id', '=', self.product_id_c.id)]} 
      print res 
     return res 

class Product_Template(models.Model): 
    _inherit = 'product.template' 

    imprint_location_ids = fields.One2many('imprint.location', 'product_id') 
    sale_imprint_charge_ids = fields.One2many('imprint.charge', 'product_id_c') 

지금 내가 product.template의 페이지를 정의하고 페이지 내에서 <tree editable="bottom">sale_imprint_charge_ids이고 내가 product_id_c 필드를 선택하고 있지 않다있다 [이 필드에 표시되지 않습니다 정의 된 트리].

지금 여기 내 문제는 내가 imprint.charge을 위해 잘 작동하지만 난 다음 product.template에서 입력 할 때 내가

TypeError: <odoo.models.NewId object at 0x7fbb8bc21b90> is not JSON serializable

을 말하는 오류가 product_filter 방법을 정의 폼보기에서이 옵션을 선택하면

에서 객체가 <odoo.models.NewId object at 0x7fbb8bc21b90>을 전달하므로 self.product_id_c 인 경우 product.template(<odoo.models.NewId object at 0x7fbb8bc21b90>)이 인쇄되므로 직렬화 할 수 없습니다. 출력을 비어있는 목록 []을주는 self.product_id_c.ids을 시도했다.

개체에서 product.template ID를 얻거나 id 자체를 전달하여 일부 방법을 재정의하는 방법은 무엇입니까?

답변

1

다음 몇 가지 사항을 개선해야합니다.

  • 입술 [ '도메인'] = { "location_id"[('PRODUCT_ID' '='self.product_id_c.id)]}
  • 입술
  • 연구 일부 검색 ORM의() 방법

다음 코드로 시도 :

@api.multi 
@api.onchange('product_id_c', 'location_id') 
def product_filter(self): 
    res = {} 
    if self.product_id_c: 

     self.location_id = False 

     #search product_template in imprint.locationwith table and limit we will get only record if related record found 
     location_id = self.env['imprint.location'].search([('product_id', '=', self.product_id_c.id)], limit=1) 

     if location_id: 

      #location_id.ids will give you something like [2] so we need to set value as 2 
      self.location_id = location_id.ids[0] 

편집 :

첫 번째 댓글에 따라 관련 위치 목록이 필요합니다. 다음으로 트릭을 따라야합니다.

  • 인쇄물에 도메인을 추가 product_filter() 방법
  • 를 제거합니다., 이후

    <field name="location_id" domain="[('product_id', '=', product_id_c)]"/> 
    

    를 다시 시작 Odoo 서버 및 사용자 정의 모듈을 업그레이드 예를 들어 개체보기 파일

을 부과합니다.

+0

필드 'location_id'를 필터링해야합니다 ... 해당 제품의 필드 만 표시하고 싶습니다. 결과는 목록이 될 수 있으며 사용자가 선택할'location_id '를 선택하길 원합니다 – maharshi

+0

여전히'product.template'에서 빈 레코드 셋을 얻습니다. 그것은'id'가 아닌'object'를 전달합니다. – maharshi

1

새로운 레코드를 만들 때 Odoo가 wierd <odoo.models.NewId object at 0x7fbb8bc21b90> 개체를 만듭니다. 레코드를 작성한 후에는이 ID가 익숙한 일반 ID (정수)로 변환됩니다. 이 상황에서 당신은 (그러한 가치가 실제로 존재하지 않는 시점에서 실제 ID 값을 기대하지 않고) 기대할 수있는 함수를 가지고 있습니다. id가 정수인지 평가하고 그 상황에서 대체 값을 제공하는 등의 대체 조치를 제공해야합니다. 귀하의 기능은 내가 당신이 무엇을 기대하고 있는지 알지 못하는 물건을 돌려주는 것처럼 보이지만. 필드 중 하나의 값을 수정하려면 객체를 반환하는 대신 자체 객체의 값을 수정합니다.

관련 문제