Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

how to set many2one field and autofill other fields?

Subscribe

Get notified when there's activity on this post

This question has been flagged
many2oneon_changeodooV8
2 Replies
15630 Views
Avatar
Uppili Arivukkannu

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


1
Avatar
Discard
Uppili Arivukkannu
Author

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

Avatar
ayman mohammed adam
Best Answer

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


         

0
Avatar
Discard
Uppili Arivukkannu
Author

Thanks for your answer. It works great.

ayman mohammed adam

Ok

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Related Posts Replies Views Activity
Autofill gives ID number not the field value? Solved
many2one on_change odooV8
Avatar
Avatar
2
Jul 16
4882
Import many2one fields through export option
export many2one odooV8
Avatar
Avatar
1
Apr 20
5805
How to have more than one many2one field for a method?
fields many2one odooV8
Avatar
Avatar
1
Jun 18
4672
AttributeError: 'bool' object has no attribute '_fields' - Odoo v8
many2one related odooV8
Avatar
1
Nov 17
8088
Read all sale.order objects on draft state from a custom module - Odoo v8 Solved
many2one sale.order odooV8
Avatar
Avatar
1
Aug 17
4960
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now