ToB企服应用市场:ToB评测及商务社交产业平台
标题:
odoo 为可编辑列表视图字段搜索添加查询过滤条件
[打印本页]
作者:
圆咕噜咕噜
时间:
2023-2-20 07:05
标题:
odoo 为可编辑列表视图字段搜索添加查询过滤条件
实践环境
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">
<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>
复制代码
修改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)
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4