Odoo-更改其他模块中的字段

时间:2018-07-25 21:16:21

标签: odoo odoo-10 odoo-view

Odoo10- 一个模块是res.partner,另一个模块是出勤。出勤时,我想编辑res.partner中的字段,例如手机,电话等。

在出勤模块中,我可以查看res.partner中的字段,但无法编辑或保存该信息。以下代码适用于查看字段,但不能编辑。我注释掉的字段是我只是测试并尝试开始工作。

partner_id = fields.Many2one('res.partner', string="Partner", default=_default_partner, required=True, ondelete='cascade', index=True)
    check_in = fields.Datetime(string="Check In", default=fields.Datetime.now, required=True)
    check_out = fields.Datetime(string="Check Out")
    worked_hours = fields.Float(string='Worked Hours', compute='_compute_worked_hours', store=True, readonly=True)
    # membership_stop = fields.Date(related='partner_id.membership_stop', string="Membership End")
    # mobile = fields.Char("Mobile")
     mobile = fields.Char(related='partner_id.mobile' , string="Mobile")
    #phone = fields.Many2one('res.partner', string="Phone", index=True)

     @api.onchange('partner_id', 'mobile', 'membership_stop')
     def _onchange_partner(self):
         self.mobile = 'self.partner_id.mobile'
         self.membership_stop = 'self.partner_id.membership_stop'

XML文件

<record id="view_attendance_tree" model="ir.ui.view">
        <field name="name">res.partner.attendance.tree</field>
        <field name="model">res.partner.attendance</field>
        <field name="arch" type="xml">
            <tree string="Partner attendances">
                <field name="partner_id"/>
                <field name="check_in"/>
                <field name="check_out"/>
                <field name="mobile"/>
                <field name="membership_stop"/>


            </tree>
        </field>
    </record>

有人可以解释如何根据出席者编辑手机号码。

1 个答案:

答案 0 :(得分:1)

按如下所示更改您的字段

partner_id = fields.Many2one('res.partner', string="Partner", default=_default_partner, required=True, ondelete='cascade', index=True)
check_in = fields.Datetime(string="Check In", default=fields.Datetime.now, required=True)
check_out = fields.Datetime(string="Check Out")
membership_stop = fields.Date(string="Membership End")
mobile = fields.Char(string="Mobile")

此变更将在变更时获得“手机号码”和“会员资格”

@api.onchange('partner_id')
def _onchange_partner(self):
    if self.partner_id:
       self.mobile = self.partner_id.mobile
       self.membership_stop = self.partner_id.membership_stop

此创建方法将在创建出勤记录时更新合作伙伴记录。

@api.model
def create(self,vals)
    if vals.get('partner_id')
       partner_rec = self.env["res.partner"].browse(vals.get('partner_id'))
       if vals.get('mobile') and vals.get('mobile') != partner_rec.mobile:
          partner_rec.mobile = vals.get('mobile')
       if vals.get('membership_stop') and vals.get('membership_stop') != partner_rec.membership_stop: 
          partner_rec.membership_stop = vals.get('membership_stop')

    #replace yourclassname
    return super(YourClassName, self).create(vals)

以下写入方法将在编辑记录时更新伙伴记录。

@api.multi
def write(self,vals)
    partner_rec = False
    if vals.get('partner_id')
       partner_rec = self.env["res.partner"].browse(vals.get('partner_id'))
    else:
       partner_rec = self.partner_id

    if vals.get('mobile') and vals.get('mobile') != partner_rec.mobile:
        partner_rec.mobile = vals.get('mobile')
    if vals.get('membership_stop') and vals.get('membership_stop') != partner_rec.membership_stop: 
         partner_rec.membership_stop = vals.get('membership_stop')

    #replace yourclassname
    return super(YourClassName, self).write(vals)