实践环境
Odoo 14.0-20221212 (Community Edition)
需求描述
如下图,列表网仓记录详情页面(form视图),编辑内联视图中的货主记录,为货主和仓库字段搜索,添加过滤条件,具体如下:
- 添加、编辑货主时,下拉列表中只展示选取和当网仓记录所属公司关联的货主,点击搜索更多,仅展示和当前网仓记录所属公司关联的货主
- 添加、编辑货主时,下拉列表中只展示选取和当网仓记录关联的仓库(到 “仓库” Tab页中添加的仓库),点击搜索更多,仅展示和当前网仓记录关联的仓库。
模型设计
说明:为了更好的体现本文主题,部分非关键代码已省略,即做了适当的模型简化处理
# 网仓 class OmsNetwork(models.Model): _name = 'oms.network' _description = 'OMS Network' company_id = fields.Many2one('res.company', '公司', index=True, tracking=3, readonly=True, default=lambda self: self.env.company) warehouse_ids = fields.Many2many('stock.warehouse', string="仓库") line_ids = fields.One2many('oms.network.line', 'network_id', string='货主') # 货主 class OmsNetworkLine(models.Model): _name = 'oms.network.line' _description = 'OMS Network Line' network_id = fields.Many2one('oms.network', string='仓网', required=True) partner_id = fields.Many2one('res.partner', string='货主', required=True) warehouse_id = fields.Many2one('stock.warehouse', string="仓库", required=True) company_id = fields.Many2one('res.company', '公司', store=True, related='network_id.company_id') class ResPartner(models.Model): _inherit = ['res.partner'] _name = 'res.partner' #...略 def _get_default_company_id(self): if self.env.context.get('set_default_company', False): return self.env.company return False company_id = fields.Many2one( # 注意,这个字段和OmsNetwork.company_id关联了相同模型,所以下文可用这个字段进行搜索过滤 'res.company', 'Company', index=True, check_company=False, tracking=3, default=lambda self: self._get_default_company_id()) # 仓库 class StockWarehouse(models.Model): _inherit = 'stock.warehouse' # ...略 # 注:没有类似ResPartner的company_id字段
视图设计
<?xml version="1.0" encoding="UTF-8" ?> <odoo> <data> <!--此处代码已省略--> <record id="view_oms_network_form" model="ir.ui.view"> <field name="name">oms.network.form</field> <field name="model">oms.network</field> <field name="priority" eval="0"/> <field name="arch" type="xml"> <form string="OMS Network"> <!--此处代码已省略--> <sheet> <group> <group> <!--此处代码已省略--> </group> <group> <field name="company_id"/> </group> </group> <notebook> <page string="货主" name="line_ids" > <field name="line_ids"> <tree editable="bottom"> <field name="partner_id"/> <field name="partner_code"/> <field name="warehouse_id"/> <!--此处代码已省略--> </tree> </field> </page> <!--此处代码已省略--> </notebook> </sheet> <!--此处代码已省略--> </form> </field> </record> <!--此处代码已省略--> </data> </odoo>
添加过滤条件代码实现
修改视图,给视图添加context
<?xml version="1.0" encoding="UTF-8" ?> <odoo> <data> <!--此处代码已省略--> <record id="view_oms_network_form" model="ir.ui.view"> <field name="name">oms.network.form</field> <field name="model">oms.network</field> <field name="priority" eval="0"/> <field name="arch" type="xml"> <form string="OMS Network"> <!--此处代码已省略--> <sheet> <group> <group> <!--此处代码已省略--> </group> <group> <field name="company_id"/> </group> </group> <notebook> <page string="货主" name="line_ids" > <field name="line_ids" context="{'oms_network_id':active_id, 'oms_network_company_id': company_id}"> <tree editable="bottom"> <field name="partner_id" context="{'oms_network_company_id':context.get('oms_network_company_id')}"/> <field name="partner_code"/> <field name="warehouse_id" context="{'oms_network_id':context.get('oms_network_id')}"/> <!--此处代码已省略--> </tree> </field> </page> <!--此处代码已省略--> </notebook> </sheet> <!--此处代码已省略--> </form> </field> </record> <!--此处代码已省略--> </data> </odoo>
修改ResPartner
,重写模型name_search
(编辑货主字段,弹出下拉列表时,会请求该模型函数),search_read
(编辑货主字段,点击下拉列表时 搜索更多打开界面时,会请求该模型函数)
提示:分析OmsNetworkLine
模型定义可知道,货主字段(partner_id
)为多对一字段,关联ResPartner
模型
class ResPartner(models.Model): _inherit = ['res.partner'] _name = 'res.partner' @api.model def name_search(self, name='', args=None, operator='ilike', limit=100): company_id = self.env.context.get('oms_network_company_id') if company_id: args = args or [] args.append(('company_id', '=', company_id)) res = super(ResPartner, self).name_search(name, args, operator, limit) return res @api.model def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None): company_id = self.env.context.get('oms_network_company_id') if company_id: domain = domain or [] domain.append(('company_id', '=', company_id)) return super(ResPartner, self).search_read(domain, fields, offset, limit, order)
修改StockWarehouse
,重写模型name_search
,search_read
提示:分析OmsNetworkLine
模型定义可知道,仓库字段(warehouse_id
)为多对多字段,关联stock.warehouse
模型
class StockWarehouse(models.Model): _inherit = 'stock.warehouse' # ...略 def _check_multiwarehouse_group(self): pass @api.model def name_search(self, name='', args=None, operator='ilike', limit=100): oms_network_id = self.env.context.get('oms_network_id') warehouse_ids = self.env['oms.network'].browse([oms_network_id]).warehouse_ids.ids if oms_network_id: args = args or [] args.append(('id', 'in', warehouse_ids)) res = super(StockWarehouse, self).name_search(name, args, operator, limit) return res @api.model def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None): oms_network_id = self.env.context.get('oms_network_id') warehouse_ids = self.env['oms.network'].browse([oms_network_id]).warehouse_ids.ids if warehouse_ids: domain = domain or [] domain.append(('id', 'in', warehouse_ids)) return super(StockWarehouse, self).search_read(domain, fields, offset, limit, order)