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 set many2one field and autofill other fields?

By
Uppili Arivukkannu
on 7/23/16, 5:15 AM 614 views

My .py code is

from openerp.osv import fields, osv

class autobuy(osv.osv):

_name = "auto.buy"

_description = "AutoBuy Customers"

_columns = {

'name' : fields.char('Year',required=True),

'make': fields.char('Make', required=True),

'model' : fields.char('Model', required=True),

'service': fields.char('Service ID'),

}

class servicesales(osv.osv):

_name = "service.sale"

_description = "service"

_columns = {

'name': fields.char('Year', required=True),

'make': fields.char('Make', required=True),

'model': fields.char('Model', required=True),

'serviceid': fields.many2one('auto.buy','Service ID', select=True),

}

My xml code is

<openerp>

<data>

<record id="auto_buy_form_view" model="ir.ui.view">

<field name="name">auto.buy.form</field>

<field name="model">auto.buy</field>

<field name="type">form</field>

<field name="arch" type="xml">

<form string="Autobuy customer details">

<sheet>

<group>

<field name="service"/>

<field name="name"/>

<field name="make"/>

<field name="model"/>

</group>

</sheet>

</form>

</field>

</record>

<record model="ir.ui.view" id="autobuy_tree_view">

<field name="name">autobuy.tree</field>

<field name="model">auto.buy</field>

<field name="type">tree</field>

<field name="arch" type="xml">

<tree string="Autobuy customer details">

<field name="service"/>

<field name="name"/>

<field name="make"/>

<field name="model"/>

</tree>

</field>

</record>

<record model="ir.actions.act_window" id="auto_buy_form">

<field name="name">Autobuy Customers</field>

<field name="res_model">auto.buy</field>

</record>

<menuitem name="AutoBuy" id="Autobuy_menu"/>

<menuitem name="Autobuy" id="AutoBuy_group_menu" parent="Autobuy_menu"/>

<menuitem name="Autobuy Customers" parent="AutoBuy_group_menu" id="Autobuy_menu_mainform" action="auto_buy_form"/>

<record id="service_form_view" model="ir.ui.view">

<field name="name">service.form</field>

<field name="model">service.sale</field>

<field name="type">form</field>

<field name="arch" type="xml">

<form string="Service">

<sheet>

<group>

<field name="serviceid"/>

<field name="name"/>

<field name="make"/>

<field name="model"/>

</group>

</sheet>

</form>

</field>

</record>

<record model="ir.ui.view" id="service_tree_view">

<field name="name">service.tree</field>

<field name="model">service.sale</field>

<field name="type">tree</field>

<field name="arch" type="xml">

<tree string="Service">

<field name="serviceid"/>

<field name="name"/>

<field name="make"/>

<field name="model"/>

</tree>

</field>

</record>

<record model="ir.actions.act_window" id="service_buy_form">

<field name="name">Service</field>

<field name="res_model">service.sale</field>

</record>

<menuitem name="Sales" id="Sales_group_menu" parent="Autobuy_menu"/>

<menuitem name="Slip" parent="Sales_group_menu" id="Sales_menu_mainform" action="service_buy_form"/>

</data>

</openerp>


I have set the field "serviceid" as many2one to call the to call the above class field "service"

'serviceid': fields.many2one('auto.buy','Service ID', select=True),

but it show the values of the "name" field of previous class and not the field "service"

How can I set this?

My second question is

I want to set a onchange function to auto fill the fields. Once I give the "serviceid" field it must call and auto fill the fields of based on the above class.

I will give all field value in the first class and once I give the "serviceid" all fields related to "serviceid" should be auto filled.

Please help me with the complete onchange function(with searching ID) and in xml.

Thanks


def onchange_serviceid(self, cr, uid,name, serviceid):

result={'value':{'make':False}}

if serviceid:

service = self.pool.get('auto.buy').browse(cr, uid, name)

result['value']={'make':service.make.id}

return result

I have tried this code to auto fill but it shows error as

result['value']={'make':service.make.id}

AttributeError: 'bool' object has no attribute 'id'

How can I solve this

Uppili Arivukkannu
on 7/23/16, 6:56 AM
0
ayman mohammed adam
On 7/23/16, 7:06 AM

dear Uppili Arivukkannu 

,

 add _rec_name= 'service'                                                       
  like this:

class autobuy(osv.osv):

_name = "auto.buy"

_rec_name= 'service'

_description = "AutoBuy Customers"

_columns = {

'name' : fields.char('Year',required=True),

'make': fields.char('Make', required=True),

'model' : fields.char('Model', required=True),

'service': fields.char('Service ID'),

}



the second question

define the onchange method like this:

def onchange_service(self, cr, uid, ids, serviceid=False, context=None):

     res= {}

     if serviceid:

         service_obj= self.pool.get('auto.buy')

         rec = service_obj.browse(cr, uid, serviceid)

         res= {'value':{'name':rec.name, 'model':rec.model, 'make':rec.make}}

     else:

         res= {'value':{'name':False, 'model':False, 'make':False}}

     return res







Then in xml call the onchange method like this:


<field name="serviceid" on_change="onchange_service(serviceid)"/>




I hoped I helped you


         

Thanks for your answer. It works great.

Uppili Arivukkannu
on 7/23/16, 7:14 AM

Ok

ayman mohammed adam
on 7/23/16, 7:45 AM

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: 7/23/16, 5:15 AM
Seen: 614 times
Last updated: 7/23/16, 7:14 AM