title: Odoo 8 domain filter on Many2one field
body: <p>I would like to make a filter on a Many2one field in Odoo 8. 
  This is for the stock module. </p>
  
  <p>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. </p>
  
  <p>This is the code for the stock.production.lot model
  When product_id is updated, then the domain filter should be changed. </p>
  
  <pre><code>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
  </code></pre>
  
  <p>This is the model to connect the additional fields model to the lot number model. 
  lot_additional_fields should be updated here. </p>
  
  <pre><code>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")
  </code></pre>
  
  <p>If someone could help me to solve this I would be very greatful. </p>
  
  <p>======================= EDIT =============================</p>
  
  <p>I have added the code to stock.production.lot</p>
  
  <pre><code>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
  </code></pre>
  
  <p>to lot.additional.fields I have added product_id:</p>
  
  <pre><code>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")
  </code></pre>
  
  <p>lot_lot_additional_fields I have left the same:</p>
  
  <pre><code>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")
  </code></pre>
  
  <p>I will also add the xml code:</p>
  
  <pre><code> &lt;record id="view_production_lot_form" model="ir.ui.view"&gt;
              &lt;field name="name"&gt;Stock Production Lot Form&lt;/field&gt;
              &lt;field name="model"&gt;stock.production.lot&lt;/field&gt;
              &lt;field name="inherit_id" ref="stock.view_production_lot_form"/&gt;
              &lt;field name="arch" type="xml"&gt;
                  &lt;page string="Products" position="after"&gt;
                      &lt;page string="Additional Fields"&gt;
                          &lt;field name="lot_lot_additional_fields" &gt;
                              &lt;tree editable="bottom"&gt;
                                  &lt;field name="lot_additional_fields"  /&gt;
                                  &lt;field name="value"/&gt;
                              &lt;/tree&gt;
                          &lt;/field&gt;
                          &lt;h3&gt;General remarks&lt;/h3&gt;
                          &lt;field name="remarks"/&gt;
                      &lt;/page&gt;
                  &lt;/page&gt;
              &lt;/field&gt;
      &lt;/record&gt;
  </code></pre>