使用 Python 复制值

Copy values with Python

我有一个包含 2 个字段的模型:

class res_partner(orm.Model):
    _inherit='res.partner'
    _columns={
        'expl_tour1':fields.many2one('member.formation.step', 'Tour 1'),
        'fast_assign':fields.many2one('member.formation.step', "Valeur d'assignation rapide"),

_def fast_assign_exploitation(self,cr,uid,ids,context=None):
            expl_tour1 = fast_assign

在我看来我有一个按钮:

<button name="fast_assign_exploitation" type="object" string="Assignation rapide" class="oe_edit_only" />

但是当我点击按钮时,出现错误:

File "/var/packages/Odoo8/target/addons/member_formation/member_formation.py", line 70, in fast_assign_exploitation
    expl_tour1 = self.fast_assign
  File "/usr/local/lib/python2.7/site-packages/openerp/fields.py", line 701, in __get__
    record.ensure_one()
  File "/usr/local/lib/python2.7/site-packages/openerp/models.py", line 5146, in ensure_one
    if len(self) == 1:
  File "/usr/local/lib/python2.7/site-packages/openerp/models.py", line 5330, in __len__
    return len(self._ids)
AttributeError: 'res.partner' object has no attribute '_ids'

我只是想把字段fast_assign的值复制到字段expl_tour1中。你知道怎么做吗?

因为 fast_assign 是 Many2one 字段,您只需要获取 ID。

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

    _columns = {
        'expl_tour1': old_fields.many2one('member.formation.step', 'Tour 1'),
        'fast_assign': old_fields.many2one('member.formation.step', "Valeur d'assignation rapide")
    }

    def fast_assign_exploitation(self, cr, uid, ids, context=None):
        partners = self.browse(cr, uid, ids, context)
        for partner in partners:
            if partner.fast_assign:
                partner.expl_tour1 = partner.fast_assign.id

并使用新的 API:

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

    expl_tour1 = fields.Many2one('member.formation.step', 'Tour 1')
    fast_assign = fields.Many2one('member.formation.step', "Valeur d'assignation rapide")

    @api.multi
    def fast_assign_exploitation(self):
        for partner in self:
            # partner.expl_tour1 to get `expl_tour1 ` value.
            if partner.fast_assign:
                partner.expl_tour1 = partner.fast_assign.id