2015-07-30 29 views
5

Trong mô-đun của tôi có lĩnh vực many2one sau: 'xx_insurance_type': fields.many2one('xx.insurance.type', string='Insurance')odoo - tên hiển thị của sự kết hợp lĩnh vực many2one của 2 lĩnh vực

nơi xx.insurance.type như sau:

class InsuranceType(osv.Model): 
    _name='xx.insurance.type' 

    _columns = { 
     'name' : fields.char(size=128, string = 'Name'), 
     'sale_ids': fields.one2many('sale.order', 'xx_insurance_type', string = 'Sale orders'), 
     'insurance_percentage' : fields.float('Insurance cost in %') 
    } 

Tôi biết lĩnh vực many2one mất Tên trường tên làm tên hiển thị của nó nhưng tôi muốn sử dụng tên này là kết hợp của nameinsurance_percentage ở dạng name + " - " + insurance_percentage + "%"

Tôi đọc nó là tốt nhất để ghi đè lên các phương pháp get_name vì vậy tôi thử như sau:

def get_name(self,cr, uid, ids, context=None): 
    if context is None: 
     context = {} 
    if isinstance(ids, (int, long)): 
     ids = [ids] 

    res = [] 
    for record in self.browse(cr, uid, ids, context=context): 
     name = record.name 
     percentage = record.insurance_percentage 
     res.append(record.id, name + " - " + percentage + "%") 
    return res 

và đặt này bên trong lớp ÌnsuranceType`. Vì không có gì xảy ra: Do i phải đặt nó bên trong những lớp học chính chứa lĩnh vực này? Nếu vậy, có cách nào khác để làm điều này vì điều đó có lẽ cũng sẽ thay đổi cách hiển thị của các trường many2one khác không?

+1

tên của phương pháp này là 'name_get' thay vì 'get_name' – ChesuCR

Trả lời

9

Nếu bạn không muốn thay đổi tên hiển thị của phần còn lại của many2one liên quan đến mô hình xx.insurance.type, bạn có thể thêm một bối cảnh trong giao diện XML với many2one có tên hiển thị mà bạn muốn thay đổi:

<field name="xx_insurance_type" context="{'special_display_name': True}"/> 

Và sau đó, trong name_get chức năng của bạn:

def name_get(self, cr, uid, ids, context=None): 
    if context is None: 
     context = {} 
    if isinstance(ids, (int, long)): 
     ids = [ids] 
    res = [] 
    if context.get('special_display_name', False): 
     for record in self.browse(cr, uid, ids, context=context): 
      name = record.name 
      percentage = record.insurance_percentage 
      res.append(record.id, name + " - " + percentage + "%") 
    else: 
     # Do a for and set here the standard display name, for example if the standard display name were name, you should do the next for 
     for record in self.browse(cr, uid, ids, context=context): 
      res.append(record.id, record.name) 
    return res 
+0

Đáng tiếc là nó cũng * bộ * tên của các hồ sơ, vì vậy nó messes lên m y cơ sở dữ liệu. –

+0

Điều này thật tuyệt. Cảm ơn người đàn ông bạn đã cứu ngày của tôi. :) – weelDaw

Các vấn đề liên quan