1
votes

Hi I am new to OpenERP7/Odoo7 and not really getting how to do this.

I have a existing records in my custom product module and I want to retrieve that record by entering only to my prodcode fields and want to autofill some fields.

I have been reading a lot on the internet, Odoo forums, but cannot find the answer or I just don't understand it. I already have post with regards to my problem So if is there someone could give me an answer on how to achieve this, or direct me to a easy to understand explanation. That would be great.

Here is I have done already

PYTHON---

class test_product(osv.Model):
    _name = "test.product"

    def name_change(self, cr, uid, ids, test_prodcode_id, prodname, desc, price, context=None):
        return {
        'value': { 
            'prodname': 'Plastic Ware',
            'desc': 'Affordable Plastic Ware',
            'price': 100.00,
        }
    }

    _columns = {
        'test_prodcode_id': fields.many2one('test.prodcode', 'Code'),   
        'prodname': fields.char('Name', size=32),        
        'desc': fields.char('Description', size=32),
        'price': fields.float('Price',),
    }

class test_prodcode(osv.Model):
    _name = "test.prodcode"

    _columns = {        
        'name': fields.char('Product Code', size=32),
    }

XML----

<record id="test_product_form_view" model="ir.ui.view">
    <field name="name">test.product.form.vew</field>
    <field name="model">test.product</field>
    <field name="type">form</field>
    <field name="arch" type="xml">
        <form string="Product" version="7.0">
            <sheet>
                <field name="test_prodcode_id" on_change="name_change(test_prodcode_id,prodname,desc,price)"/>
                  <field name="prodname"/>
                  <field name="desc"/>
                  <field name="price"/>
            </sheet>
        </form>
    </field>
</record>
2
any expert here to help me to achieve this?user3740506
Hi all, Is there someone help me on this til now I can't make it work auto fill some field.user3740506

2 Answers

1
votes

Yeah, I've had this problem too.

I've got the solution. Auto fill can be achieved by doing

_defaults = {}

, or, as in your example, by doing

class test_product(osv.Model):
_columns = {
        'test_prodcode_id': fields.many2one('test.prodcode', 'Code'),   
        'prodname': fields.char('Name', size=32),        
        'desc': fields.char('Description', size=32),
        'price': fields.float('Price',),
    }

_defaults: {
 'test_prodcode_id' : 'defaults_prodcode',
}

You can also assign a function that return the value of the defaults.

1
votes
  • I have solution but it is not as exactly as you want but you can do like this also.
  • You can use related for auto fill your other fields. Suppose you set product_id it will auto fill product_name, desc and price regarding to your product_id.
  • You can also see Example here : https://stackoverflow.com/a/42453539/5161074