gpt4 book ai didi

python - Many2one 字段上的 Odoo 8 域过滤器

转载 作者:行者123 更新时间:2023-11-30 23:07:43 28 4
gpt4 key购买 nike

我想在 Odoo 8 中的 Many2one 字段上进行过滤。这是用于库存模块的。

我创建了额外的字段,用户可以在其中输入附加信息。现在我们要过滤下拉列表中显示的可用字段,或者单击“更多”按钮时仅显示该产品的附加字段。

这是stock.Production.lot模型的代码当product_id更新时,应该更改域过滤器。

class StockProductionLot(models.Model):
_inherit = "stock.production.lot"
lot_lot_additional_fields = fields.One2many("lot.lot.additional.fields", "stock_production_lot")

@api.onchange("product_id")
def onchange_product_id(self):
res = {}
cat_fields = self.product_id.categ_id.lot_fields_category.lot_additional_fields
ids = []
for item in cat_fields:
ids.append(item.id)
if self.product_id:
res["domain"] = {"lot_additional_fields": [("id", "in", "ids")]}
return res

这是将附加字段模型连接到批号模型的模型。lot_additional_fields 应在此处更新。

class LotLotAdditionalFields(models.Model):
_name = "lot.lot.additional.fields"
value = fields.Char("Value")
stock_production_lot = fields.Many2one("stock.production.lot", string="Lot/Serial Number")
lot_additional_fields = fields.Many2one("lot.additional.fields", string="Additional field")

如果有人能帮助我解决这个问题,我将非常感激。

========================编辑====================== ======

我已将代码添加到 stock.Production.lot

class StockProductionLot(models.Model):
_inherit = "stock.production.lot"
lot_lot_additional_fields = fields.One2many("lot.lot.additional.fields", "stock_production_lot")
lot_additional_fields = fields.Many2one("lot.additional.fields", string="Lot additional fields")
remarks = fields.Html("General remarks")

@api.onchange('product_id')
def onchange_product_id(self):
res = {}
if self.product_id:
ids = self.product_id.categ_id.lot_fields_category.lot_additional_fields.mapped('id')
res['domain'] = {'lot_lot_additional_fields': [('lot_additional_fields', 'in', ids)]}
return res

在lot.additional.fields中我添加了product_id:

class LotAdditionalFields(models.Model):
_name = "lot.additional.fields"
name = fields.Char("Name", required=True)
type = fields.Selection(
[("int", "Numerical"), ("string", "Alfanumerical"), ("boolean", "Checkbox"), ("date", "Date")], required=True)
mandatory = fields.Boolean("Mandatory field")
display_order = fields.Integer("Display Order")
lot_field_category = fields.Many2many("lot.field.category", string="Field Category", required=True)
lot_lot_additional_fields = fields.One2many("lot.lot.additional.fields", "lot_additional_fields")
product_id = fields.Many2one("product.product", string="Product ID")

lot_lot_additional_fields 我留下了相同的:

class LotLotAdditionalFields(models.Model):
_name = "lot.lot.additional.fields"
value = fields.Char("Value")
stock_production_lot = fields.Many2one("stock.production.lot", string="Lot/Serial Number")
lot_additional_fields = fields.Many2one("lot.additional.fields", string="Additional field")

我还将添加 xml 代码:

 <record id="view_production_lot_form" model="ir.ui.view">
<field name="name">Stock Production Lot Form</field>
<field name="model">stock.production.lot</field>
<field name="inherit_id" ref="stock.view_production_lot_form"/>
<field name="arch" type="xml">
<page string="Products" position="after">
<page string="Additional Fields">
<field name="lot_lot_additional_fields" >
<tree editable="bottom">
<field name="lot_additional_fields" />
<field name="value"/>
</tree>
</field>
<h3>General remarks</h3>
<field name="remarks"/>
</page>
</page>
</field>
</record>

最佳答案

您必须考虑到 res["domain"] = {"lot_additional_fields": [("id", "in", "ids")]} 行中的内容您正在更改模型的域 stock.production.lot ,顺便说一句,似乎没有任何名为 lot_additional_fields 的字段(它有一个名为 lot_lot_additional_fields 的字段)。

@api.onchange('product_id')
def onchange_product_id(self):
res = {}
if self.product_id:
ids = self.product_id.categ_id.lot_fields_category.lot_additional_fields.mapped('id')
res['domain'] = {'lot_lot_additional_fields': [('lot_additional_fields', 'in', ids)]}
return res

我正在阅读您的代码,现在我确信我在这里编写的代码是您想要在尝试中获得的正确代码。但是,我没有任何有关 View 和类的信息 lot.additional.fields ,仅关于stock.production.lotlot.lot.additional.fields ,我想(如果我很好地理解你想要什么)这不会起作用(修改 View 中的域不会修改其他不同 View 的域),除非你有 Many2one字段指向 product_idlot.additional.fields类(我在这里看不到)。

编辑

好的,如果我错了,请先纠正我:在你的 stock_production_lot 中表单 View 中,您看到字段lot_lot_additional_fields的树。在这里,如果您打开字段 lot_additional_fields 的下拉菜单,您只希望能够选择那些 lot_additional_fields属于同一个lot_fields_category categ_idproduct_id的当前stock_production_lot ,不是吗?

在这种情况下,请尝试以下代码:

Python(将此 onchange 放入您的 lot.lot.additional.fields 类中)

@api.onchange('stock_production_lot')
def onchange_stock_production_lot(self):
res = {}
if stock_production_lot:
ids = self.stock_production_lot.product_id.categ_id.lot_fields_category.lot_additional_fields.mapped('id')
res['domain'] = {
'lot_additional_fields': [('id', 'in', ids)]}
}
return res

XML(修改您的 view_production_lot_form 代码并编写此代码)

<record id="view_production_lot_form" model="ir.ui.view">
<field name="name">Stock Production Lot Form</field>
<field name="model">stock.production.lot</field>
<field name="inherit_id" ref="stock.view_production_lot_form"/>
<field name="arch" type="xml">
<page string="Products" position="after">
<page string="Additional Fields">
<field name="lot_lot_additional_fields" widget="one2many_list" context="{'default_stock_production_lot': active_id}">
<tree editable="bottom">
<field name="stock_production_lot" invisible="1"/>
<field name="lot_additional_fields"/>
<field name="value"/>
</tree>
</field>
<h3>General remarks</h3>
<field name="remarks"/>
</page>
</page>
</field>
</record>

关于python - Many2one 字段上的 Odoo 8 域过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32068611/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com