1
votes

I'm creating a new module on Odoo (OpenERP) but I can't use a field correctly.

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

I can select a field from another class (defined in the same module) but i need to change the value displayed.

In the example I have the string "menu,2", for the field piatto. This string is obtained from the class ordini, with a many2one field, but i want to display the field named nome (showed in the following image).

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

This is the python file.

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,
}

EDIT I also need to update the field prezzo when i select the piatto entry.

Thanks.

1

1 Answers

6
votes

You need to declare which field is used for the object name, using the _rec_name attribute. In your case:

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

    # ...

Alternatively, you could just rename nome to name, since name is the default value for _rec_name.