Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to create a 'onchange' with many2one. [Solved]

By
Juan Carlos
on 12/18/14, 5:25 PM 1,485 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...

 

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

Juan Carlos
on 12/19/14, 9:59 AM

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

Juan Carlos
on 12/19/14, 11:05 AM
3
Harsh Dhaduk
On 12/19/14, 1:05 AM

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

1

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 12/18/14, 8:29 PM

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"}}.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 12/18/14, 5:25 PM
Seen: 1485 times
Last updated: 3/16/15, 8:10 AM