Skip to Content
Menu
This question has been flagged
5 Replies
13741 Views

Hi, Juan Carlos again...

As you know, I'm new in Odoo, and I would learn a lot.

Ok I'm tryin to create a On change with my field many2one, I try to learn onchange, but is very difficult for me, help please.

my.py

class DetalleFactura(models.Model):
    _name = "openacademy.detalle"

    def onchange_producto_id(self,cr,uid,ids, producto_id,context=None):
        precio = self.pool.get('product.product', 'price')
        return {precio}

    name = fields.Char(string="Titulo")
    producto_id = fields.Many2one('product.product', 'Producto')
    precio = fields.Float(string="Precio", readonly=True)

my.xml

<tree editable="bottom">

  <field name="producto_id"/>

  <field name="precio" on_change="onchange_producto_id(producto_id)"/>

</tree>

I use this code, but don't change the price value...

 

Avatar
Discard
Author

Thanks for the answers, I have an error with def onchange_productp_id(self): File "/opt/odoo/odoo/addons/openacademy/models.py", line 47 def onchange_producto_id(self): ^ IndentationError: unindent does not match any outer indentation level

Author

Already solved it, sorry, and how I get the price of the product. I don't understand where is the parameters for on change functions... I read in ORM instructions, but is not all the information. My code: @api.onchange('producto_id') def _onchange_producto_id(self): if self.producto_id: self.precio = 100.00

Best Answer

HI Juan,

In Odoo 8 Api following example help you to basic understand of how's work on change method and you not need to write in xml file.

my.py

class DetalleFactura(models.Model):
    _name = "openacademy.detalle"

    @api.onchange('producto_id')
    def onchange_producto_id(self):
        # Your Code but if you want to use any field use with self.producto_id like that also you can use all fields using self.<your field name>
        # About return type in this you can not return any value but you simply set your value like
        # self.precio = 100.00
        self.precio = 100.00

    name = fields.Char(string="Titulo")
    producto_id = fields.Many2one('product.product', 'Producto')
    precio = fields.Float(string="Precio", readonly=True)


my.xml

<tree editable="bottom">
  <field name="producto_id"/>
  <field name="precio" />
</tree>


if you have to learn about new API then visit official documentation for Odoo 8 Here
 


Best,
Harsh Dhaduk

Avatar
Discard
Best Answer

onchange need to return a dictionary in this format: {'value': {'field1': value1, 'field2': value 2}, 'domain': {'field-a': domain-a, 'field-b': domain-b}, 'warning': {'title': 'This is a warning', 'message': "You have done something terribly wrong"}}.

Avatar
Discard
Best Answer

 Look into Example:

campus_id = fields.Many2one('model.campus', string="Campus Name")
department_id = fields.Many2one('model.department', string="Department Name")

I have two many2one fields (campus_id and department_id), and we want to change the department on the basis of campus field. 

Todo so we use onchange method and in onchange method we need to return domain.

1     @api.onchange('campus_id')
2    def _campus_onchange(self):
3        res = {}
4        res['domain']={'department_id':[('campus_id', '=', self.campus_id.id)]}
5        return res

For more information read: http://learnopenerp.blogspot.com/2016/10/onchange-many2one-filed-in-odoo.html

Avatar
Discard
Related Posts Replies Views Activity
1
Oct 23
747
2
Mar 15
5617
0
Jul 24
102
2
Apr 24
345
1
Apr 23
2638