I would like to make a filter on a Many2one field in Odoo 8. This is for the stock module.
I have created extra fields where the user can enter additional information. Now we want to filter the available fields that appear in the dropdown, or when clicking the "more" butten to show only the additional fields for that product.
This is the code for the stock.production.lot model When product_id is updated, then the domain filter should be changed.
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
This is the model to connect the additional fields model to the lot number model. lot_additional_fields should be updated here.
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")
If someone could help me to solve this I would be very greatful.
======================= EDIT =============================
I have added the code to 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
to lot.additional.fields I have added 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 I have left the same:
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")
I will also add the xml code:
<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>