Skip to main content
 首页 » 编程设计

python之如何更改 OpenERP (Odoo) 中 many2one 字段的显示值

2025年05月04日16kevingrace

我正在 Odoo (OpenERP) 上创建一个新模块,但我无法正确使用字段。

( http://i.stack.imgur.com/pKQpT.png )

我可以从另一个类(在同一模块中定义)中选择一个字段,但我需要更改显示的值。

在示例中,我为字段piatto 设置了字符串“menu,2”。这个字符串是从ordini类中获取的,有一个many2one字段,但我想显示名为nome的字段(如下图所示) ).

( http://i.stack.imgur.com/nQwRL.png )

这是 python 文件。

class menu(osv.Model): 
_name = "menu" 
_description = "Menu" 
_order = "tipo" 
_columns = { 
    'nome': fields.char('Nome', size=80, required=True), 
    'tipo': fields.selection([ 
                ('antipasto', 'antipasto'), 
                ('primo', 'primo piatto'), 
                ('secondo', 'secondo piatto'), 
                ('contorno', 'contorno'), 
                ('dolce', 'dolce') 
                ], 'Tipo di piatto'), 
    'prezzo': fields.float('Prezzo', digits=(10,2), required=True), 
    'ingredienti': fields.text('Lista ingredienti'), 
    'immagine': fields.binary('Immagine'), 
} 
_sql_constraints = [('unique_name', 'unique(nome)', 'Il piatto è già presente.')] 
 
class ordini(osv.Model): 
_name = "ordini" 
_description = "Ordini" 
_columns = { 
    'dipendente': fields.many2one('hr.employee', 'Dipendente', ondelete='set null', required=True), 
    'dettagli_ids': fields.one2many('ordini.dettagli', 'n_ordine', 'Ordine'),  
} 
 
class ordini_dettagli(osv.Model):         
_name = "ordini.dettagli" 
_description = "Dettagli ordine" 
_columns = { 
    'n_ordine': fields.integer('Ordine', readonly=True), 
    'piatto': fields.many2one('menu', 'Piatto'), 
    'qta': fields.integer('Quantità'), 
    'prezzo_piatto': fields.related('piatto','prezzo',type='float',string='Prezzo',readonly=True), 
} 
_defaults = { 
     'qta': 1, 
} 

编辑 当我选择 piatto 条目时,我还需要更新字段 prezzo。

谢谢。

请您参考如下方法:

您需要使用 _rec_name attribute 声明对象名称使用哪个字段.在你的情况下:

class menu(osv.Model): 
    _name = "menu" 
    _description = "Menu" 
    _order = "tipo" 
    _rec_name = 'nome' 
 
    # ... 

或者,您可以将 nome 重命名为 name,因为 name 是 _rec_name 的默认值。