Many2one字段

时间:2015-08-18 09:30:32

标签: python python-2.7 odoo-8 odoo many-to-one

我想在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>

1 个答案:

答案 0 :(得分:3)

您必须考虑在res["domain"] = {"lot_additional_fields": [("id", "in", "ids")]}行中您正在改变模型stock.production.lot的域,顺便说一句,它似乎没有任何名为{{1}的字段(它有一个名为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 的任何信息,只有lot.additional.fieldsstock.production.lot,我猜(如果我理解你想要的是什么)这是不起作用(修改视图中的域不会修改其他不同视图的域),除非lot.lot.additional.fields类中有Many2one字段指向product_id我在这里看不到。)

修改

好的,首先纠正我,如果我错了:在lot.additional.fields表单视图中,您会看到字段stock_production_lot的树。在此处,如果您打开字段lot_lot_additional_fields的下拉列表,则只希望能够选择属于lot_additional_fields lot_additional_fields lot_fields_category的{​​{1}}个categ_id当前product_id的{​​1}},不是吗?

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

Python(将此更改放在stock_production_lot类内)

lot.lot.additional.fields

XML(修改您的@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 代码并编写此代码)

view_production_lot_form