我想将res.partner地址中的实际城市字段更改为many2one字段而不添加新字段

时间:2019-04-18 10:47:10

标签: python xml odoo odoo-8

我的代码如下:

# -*- encoding: utf-8 -*-

from openerp import models,fields, api

class ResPartner(models.Model):
    _name = 'res.partner'
    _inherit = 'res.partner'

    city_id = fields.Many2one('res.city','Ville',stored = True 
                               ,ondelete='restrict')

class ResCity(models.Model):
    _name = "res.city"
    _description = "Ville"

    name = fields.Char(u"Nom",required = True)
    state_id = fields.Many2one("res.country.state", 'Zone',ondelete='restrict')

所以我在xml中添加了视图之后,但我的要求是我需要更改实际字段而不添加新字段 旧的:city = fields.char,新的应为:city = fields.many2one

2 个答案:

答案 0 :(得分:0)

只需覆盖旧API中定义的旧API city字段:

from openerp import models,fields, api
from openerp.osv import fields as ofields

class ResPartner(models.Model):
    _inherit = 'res.partner'

    _columns = {
        'city': ofields.many2one('res.city', 'Ville', ondelete='restrict'),
    }

请记住,很多视图将不再起作用,例如伙伴看板视图。

答案 1 :(得分:0)

您可以简单地覆盖定义,但是它将破坏使用该字段的每个模型或视图,而是可以将其设为函数字段:

class ResPartner(models.Model):
    _name = 'res.partner'
    _inherit = 'res.partner'

    city_id = fields.Many2one('res.city','Ville' 
                               ,ondelete='restrict')
    @api.depends('city_id')
    @api.multi
    def set_city(self):
         self.city=self.city_id.name

    city = fields.Char(compute=set_city, store=True)

在表单视图中,您应使用添加city_id并隐藏city,其余部分保持和平...