我试图显示具有多个BoM的某些产品的视图。我创建了一个计算字段,用于标记应该显示哪些记录。我正试图创建一个搜索功能,以便感兴趣的记录可以显示为过滤器,但我在创建该功能时遇到麻烦。
当前正在尝试追加记录。将感兴趣的Id放入列表中并返回搜索域内的列表,但这不起作用。任何帮助都将非常感激。请参阅下面的代码,并提前感谢!
我尝试了下面的代码,但它返回一个空数据列表。我认为我如何获得当前记录的id并将其附加到返回的列表中有问题。
class products_ppa_bom_check(models.Model):
_inherit = ['product.template']
ppa_multi_bom = fields.Selection([
('true', 'True'),
('false', 'False'),
('na', 'Not Applicable')],
string="PPA Multi BOM Check", compute='_compute_ppa_multi_bom',
search='_search_ppa_multi_bom')
def _compute_ppa_multi_bom(self):
for record in self:
count = record.env['mrp.bom'].search_count(['|', ('product_tmpl_id', '=', record.id), ('byproduct_ids.product_id.product_tmpl_id', '=', record.id)])
if (count > 1) and ('PPA' in str(record.default_code)):
record.ppa_multi_bom = 'true'
elif (count == 1) and ('PPA' in str(record.default_code)):
record.ppa_multi_bom = 'false'
else: record.ppa_multi_bom = 'na'
def _search_ppa_multi_bom(self, operator, value):
ids = []
for record in self:
count = record.env['mrp.bom'].search_count(['|', ('product_tmpl_id', '=', record.id), ('byproduct_ids.product_id.product_tmpl_id', '=', record.id)])
if (count > 1) and ('PPA' in str(record.default_code)):
ids = ids.append(record.id)
return[('id', 'in', ids)]
如果您想在具有=
运算符的产品中使用过滤器,您可以使用下面我已经测试过的代码:
可以使用bom_count
字段而不是search_count
方法
from odoo import api, fields, models, _
class products_ppa_bom_check(models.Model):
_inherit = ['product.template']
ppa_multi_bom = fields.Selection([
('true', 'True'),
('false', 'False'),
('na', 'Not Applicable')],
string="PPA Multi BOM Check", compute='_compute_ppa_multi_bom',
search='_search_ppa_multi_bom')
def _compute_ppa_multi_bom(self):
for record in self:
if (record.bom_count > 1) and ('PPA' in str(record.default_code)):
record.ppa_multi_bom = 'true'
elif (record.bom_count == 1) and ('PPA' in str(record.default_code)):
record.ppa_multi_bom = 'false'
else:
record.ppa_multi_bom = 'na'
def _search_ppa_multi_bom(self, operator, value):
true_ids = self.env['product.template'].search([]).filtered(
lambda x: x.bom_count > 1 and 'PPA' in str(x.default_code)).ids
false_ids = self.env['product.template'].search([]).filtered(
lambda x: x.bom_count == 1 and x.default_code and 'PPA' in x.default_code).ids
if value == 'true':
ids = true_ids
elif value == 'false':
ids = false_ids
else:
all_ids = self.env['product.template'].search([]).ids
ids = list(set(all_ids) - set(true_ids + false_ids))
return [('id', 'in', ids)]
为搜索视图添加过滤器:
<record id="product_template_search_view_inherit_bom" model="ir.ui.view">
<field name="name">product.template.search.inherit.bom</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_search_view"/>
<field name="arch" type="xml">
<xpath expr="//filter[@name='consumable']" position="after">
<separator/>
<filter string="Multi BOM" name="ppa_multi_bom" domain="[('ppa_multi_bom', '=', 'true')]"/>
<filter string="One BOM" name="ppa_one_bom" domain="[('ppa_multi_bom', '=', 'false')]"/>
<filter string="NA BOM" name="ppa_na_bom" domain="[('ppa_multi_bom', '=', 'na')]"/>
</xpath>
</field>
</record>