python - odoo many2one 作为选择字段

标签 python odoo

我正在 odoo 中创建一个模块,我想使用 many2one 作为选择字段,问题出在城市名称 'ville en francais' 下拉列表有效并且它保存城市的 id(来自数据库) 但名称类似于 obj.ville,1(它显示模块的名称,id 而不是数据库中的城市名称)

table output

enter image description here

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

最佳答案

您可以按如下方式使用 _rec_name:

酒店型号:

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

威乐模型:

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

关于python - odoo many2one 作为选择字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49087063/

相关文章:

python - 使用 wsgi 日志记录不起作用,没有创建文件

odoo - 如何解决odoo14中的错误 "psycopg2.ProgrammingError: relation "base_registry_signaling“已经存在- -”?

python - Odoo 不显示 xpath 中的字段标签

python - 出现错误 : TemplateAssertionError: no filter named 'n' when printing report?

python - 在 openerp 版本 6.1 中有 aeroo 报告的错误

python - OpenERP 6,Aptana - 调试器不会在 QR 条形码标签代码中的断点处停止

python - 从 3 开始遍历列表

python - Python 中的小数位

python - 无法从Python中的列表列表中删除字符串

python - 显示 python sleep 函数的倒计时