Odoo v9 - Set a dynamic domain when EDITING a form (not creating)
<p>Lets say you are setting a field called "Animal Type", and there is a field dependent upon this called "Favorite toy".  If the "Animal Type" is a dog, I'd want to set the domain of "Favorite toy" to something like ('isdogtoy','=',True). If its a cat, then maybe we set it to False or some other condition.</p>

<p>Normally to set dynamic domains, you'd use onchange, and then set the domain for the field.</p>

<p>However, there are cases where nothing changes. For instance, if you edit an existing record, onchange isn't necessarily called. If I never change the "Animal Type", then my domain on "Favorite Toy" is never set through my onchange method.  </p>

<p>I'm not sure how we do this dynamically in Odoo.  It seems like there obviously should be a way, but I can't find anything on this.    </p>

<p>This is the best that I could come up with, using a computed field. Here is an example solution from my code</p>

<p>In my XML,</p>

<pre><code> &lt;field name="uom_id" position="replace"&gt;
      &lt;!-- The category_id.name is really only used to filter when islocaluom=True. The result is that if a uom_class is used, only uom's from that class can be selected.  Otherwise, the default uom's are present --&gt;

      &lt;field name="uom_id" groups="product.group_uom" domain="['&amp;amp;',('islocaluom','=',calcislocaluom),'|',('islocaluom','=',False),('category_id','=',calccatidname)]" options="{'no_create' : True},{'no_create_edit' : True}" /&gt;
  &lt;/field&gt;
</code></pre>

<p>Now I just created some computed fields using store=True, and then set them in the compute functions.</p>

<pre><code>class ProductTemplate(models.Model):
  _inherit = 'product.template'
  #This field will let us choose if we are using per product uom on the product
  uom_class = fields.Many2one('productuom.class', 'Per Product UOM Conversion Class', ondelete='restrict',required=False, help="Unit of Measure class for Per Product UOM")
  #These computed fields are for calculating the domain on a form edit
  calcislocaluom = fields.Boolean('Find if its a localuom',compute='_computelocaluom', store=True, default=False)
  calccatidname = fields.Char('Find the name of the category id', compute='_computecatidname', store=True,default=True)
  #[...] other code removed

  @api.one
  @api.depends('uom_class')
  def _computelocaluom(self):
      if (self.uom_class):
          self.calcislocaluom = True
          return True
      else:
          self.calcislocaluom = False
          return False

  @api.one
  @api.depends('uom_class')
  def _computecatidname(self):
      if (self.uom_class):
          self.calccatidname = self.uom_class.name
          return self.uom_class.name
      else:
          #Due to the conditions we later impose within the view, we need to specify a category name that will always be there
          self.calccatidname = "Unsorted/Imported Units"
          return True
</code></pre>

<p>I'm going to hold off on marking this as a correct answer, because its hard enough to create statically defined domains that actually do what I want them to, and act dynamically depending on the data.... but having to make these complicated statements in reverse polish notation is just torture.</p>

python openerp views