从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 (将#修改为@)