微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从odoo关系表中获取记录

如何解决从odoo关系表中获取记录

我的 odoo 项目中有两个模型。员工和设备,如下图所示。

设备/模型.py

    from openerp import models,fields,api
    import datetime
    
    
    class equipment(models.Model):
        _name = 'equipment.equipment'
    
        name = fields.Char(string='Name',)
        date_of_purchase = fields.Date(string='Date Of Purchase',default=fields.Date.today(),)
        write_off_days = fields.Integer(string="Days To Write-off",required=True,)
        write_off_date = fields.Date(string="Write-off Date",compute="_get_write_off_date",)
        price = fields.Float(string="Price '$'",)
        description = fields.Char(string="Description",required=False,)
    
        employee_id = fields.Char(string="Owner",compute="_get_owner",)
    
        @api.one
        @api.depends('employee_id')
        def _get_owner(self):
            //result = self.env.['res.equipment_table'].
            //get id from relation database                        <-------------
    
        @api.one
        @api.depends('write_off_days','date_of_purchase')
        def _get_write_off_date(self):
            date = datetime.datetime.strptime(self.date_of_purchase,"%Y-%m-%d")
            self.write_off_date = date + datetime.timedelta(days=self.write_off_days)

员工/model.py

from openerp import models,api


class employee(models.Model):
    _name = 'employee.employee'

    name = fields.Char(string='First Name')
    last_name = fields.Char(string='Last Name')
    birth_date = fields.Date(string='Birth Date',)

    equipment_ids = fields.Many2many(string="Equipment",comodel_name="equipment.equipment",relation="equipment_table",)

    total_equipment_price = fields.Float(string="Total Equipment Price '$'",compute="_get_total_equipment_price",)

    @api.one
    @api.depends('equipment_ids')
    def _get_total_equipment_price(self):
        total = 0
        for equipment in self.equipment_ids:
            total += equipment.price
        self.total_equipment_price = total

我有 many2many 字段,其中包含员工拥有的所有设备。每次更改字段时,我都需要更新设备所有者。这样做的原因是......当用户向员工添加新设备时,应该只显示未拥有的设备。这就是为什么我需要检查和更新所有者。

我已经创建了一个域来检查设备是否已经有所有者,如下所示。只需要以某种方式更新该 employee_id 字段。

     <notebook>
         <page string="Equipment">
             <group>
                 <field name="equipment_ids" domain="[('employee_id','=',False)]"/>
             </group>
         </page>
     </notebook>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。