从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 '$'",  required=True, )
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)

员工/型号.py

from openerp import models, fields, 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', default=fields.Date.today(), )
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

我有很多场地,里面有员工所有的设备。每次字段更改时,我都需要更新设备所有者。原因是…当用户向员工添加新设备时,应该只显示无主设备。这就是为什么我需要检查并更新所有者。

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

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

employee_id的类型更改为Many2one:

employee_id = fields.Many2one('employee.employee', string="Owner")

每次向列表中添加新设备时,覆盖createwrite方法以设置employee_id

class Employee(models.Model):
_name = 'employee.employee'
@api.model
def create(self, values):
res = super(Employee, self).create(values)
if res.equipment_ids:
res.equipment_ids.write({'employee_id': res.id})
return res
@api.multi
def write(self, values):
res = super(Employee, self).write(values)
for r in self:
equipment_ids = r.equipment_ids.filtered(lambda s: not s.employee_id)
if equipment_ids:
equipment_ids.write({'employee_id': r.id})
return res

因为一台设备可以设置为一个所有者,我建议您将equipment_ids的类型更改为One2many,以便能够按员工设置设备列表,并且当您将设备添加到列表时,employee_id字段值将自动为您设置。

如果更改employee_id的值,设备将自动显示在新员工的设备列表中。

equipment_ids = fields.One2many("equipment.equipment", 'employee_id', string="Equipments")  

重置所有者

@api.multi
def write(self, values):
removed_ids = set()
old_ids = {r.id: r.equipment_ids.ids for r in self}
res = super(Employee, self).write(values)
for r in self:
equipment_ids = r.equipment_ids.filtered(lambda s: not s.employee_id)
if equipment_ids:
equipment_ids.write({'employee_id': r.id})
removed_ids |= set(old_ids[r.id]) - set(r.equipment_ids.ids)
self.equipment_ids.browse(removed_ids).write({'employee_id': False})
return res

相关内容

  • 没有找到相关文章

最新更新