2015-07-30 1 views
5

В моем модуле я иметь следующее many2one поле: 'xx_insurance_type': fields.many2one('xx.insurance.type', string='Insurance')odoo - отображаемое имя many2one поля комбинации 2-х полей

где xx.insurance.type является следующее:

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 %') 
    } 

Я знаю, что поле many2one принимает имя поле как его отображаемое имя, но я хотел бы использовать его в комбинации name и insurance_percentage в форме name + " - " + insurance_percentage + "%"

Я прочитал это лучше переписать метод get_name поэтому я попытался следующее:

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 

и разместил внутри класса ÌnsuranceType`. Поскольку ничего не произошло: Должен ли я разместить его внутри основного класса, содержащего поле? Если да, есть ли другой способ сделать это, так как это, вероятно, также изменит способы отображения других многозонных полей?

+1

название метода 'name_get' вместо' get_name' – ChesuCR

ответ

9

Если вы не хотите, чтобы изменить отображаемое имя остальной части many2one связанной с моделью xx.insurance.type, вы можете добавить контекст в представлении XML в many2one, на дисплее имя, которое вы хотите изменить:

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

И затем, в вашей name_get функции:

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

к сожалению, это также * наборы * имя записи, поэтому он путает м y. –

+0

Это замечательно. Спасибо человеку, что вы спасли мой день. :) – weelDaw

 Смежные вопросы

  • Нет связанных вопросов^_^