简体   繁体   English

odoo many2one作为选择字段

[英]odoo many2one as selection field

I'm creating a module in odoo and i wanna use a many2one as a selection field, the problem is on the name of the city 'ville en francais' the dropdown works and it saves the id of the city(from database) but the name is like obj.ville,1 (it shows the name of the module,id instead of the name of the city in database) 我在odoo中创建一个模块,我想使用many2one作为选择字段,问题出在城市'ville en francais'的名称上,下拉菜单有效,并且保存了该城市的ID(来自数据库),但是名称类似于obj.ville,1(它显示模块的名称,而不是数据库中城市的名称)

表输出

在此处输入图片说明

 class obj_ghotel(osv.osv): _name = "obj.ghotel" _description = "Objet ghotel" def _get_selection(self, cr, uid, context = None): ville_obj = self.pool.get('obj.ville') ville_obj = self.env['obj.ville'] res = [] ville_ids = ville_obj.search(cr, uid, [], context = context) for g in ville_obj.browse(cr, uid, ville_ids, context = context): res.append((g.id, g.nom_ville)) return res _columns = { 'nom_hotel': fields.char( "Nom d'hotel", required=True ), 'adr_hotel': fields.char( "Addresse hotel", required=True ), 'id_ville':fields.many2one('obj.ville','ville', selection=_get_selection), #fields.integer('Id Ville', required=True ), #Foreign key references ville(id) 'id_ville':fields.many2one('ville','ville'), 'image': fields.binary( "Image" ), } 

class obj_ville(osv.osv):
    _name = "obj.ville"
    _description = "Objet ville"


    _columns = {
        'nom_ville': fields.char( "Nom Ville", required=True ),
        'id_hotel':fields.one2many('obj.ghotel','id_ville'),
    }


    _defaults = {
        'Nom Ville': "Name",
    }

You can use _rec_name as following: 您可以使用_rec_name ,如下所示:

Hotel model: 酒店型号:

class obj_ghotel(osv.osv):
    _name = "obj.ghotel"
    _description = "Objet ghotel"
    _rec_name = 'nom_hotel'

Ville model: 维尔模型:

class obj_ville(osv.osv):
    _name = "obj.ville"
    _description = "Objet ville"
    _rec_name = 'nom_ville'

This problem is when odoo don't know how to represent the record, he show you by default the name of the model and the id of the record. 问题是当odoo不知道如何表示记录时,默认情况下,他会向您显示模型的名称和记录的ID。

How odoo know how to represent the record, this is don in name_get method defined in models.py first he looks for the value of _rec_name by default is name some i'm assuming that you don't have a field name in the model obj.ville , je pense que t'as ulilisé nom ^^. odoo如何知道如何表示记录,这是在models.py定义的name_get方法中的don。首先,他寻找_rec_name的值,默认情况下是name我假设您在模型obj.ville没有字段名obj.ville ,JE pense阙t'asulilisé nom ^^。

so you need to change the _rec_name : 因此您需要更改_rec_name

 class Ville(models.Model):
    _name = 'obj.ville'
    _rec_name = 'nom'

you need to define a method called name_get like the following 您需要定义一个名为name_get的方法,如下所示

def name_get(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.nom_ville
        res.append((record.id, name))
    return res

Many2one fields are by default kind of selection fields. 默认情况下,Many2one字段是选择字段的种类。 At first you create a model for ville with 'name' as an attribute 首先,您以'name'为属性为ville创建模型

class OdooVillePerso(models.Model):
    _name = 'odoo.ville.perso'

    name = fields.Char('Ville')

class OdooFinalClass(models.Model):
    _name='odoo.final.class'

    ...

    ville = fields.Many2one('odoo.ville.perso', 'Ville')

After that you can just call it in another model as a Many2one and it will appear as selection field but don't forget to creata a view for the model. 之后,您可以在另一个模型中将其称为Many2one,它会显示为选择字段,但不要忘记为该模型创建视图。

PS: This code works on odoo 10 PS:此代码适用于odoo 10

更好的是,您可以在XML no_open和no_edit中添加选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM