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

One2many onchange in ODOO

Subscribe

Get notified when there's activity on this post

This question has been flagged
one2manyonchange
3 Replies
14900 Views
Avatar
sridhar

I having a form with one2many field, in that one2many it having 10 line items, in the form I update some sequence number to that one2many, if I change the value in 5th line item that sequences need to update 6th to 10th line item how to do that in ODOO.

@Axel Mendoza, In this image I change the value 6 as 15 then next records are need to change automatically. How to do that.

class xform(osv.osv):
 _name = 'xform'
 _columns = {
 'name': fields.char('Name', size=64),
 'xstate': fields.text('XState'),
 'xmany_ids': fields.one2many('xmany', 'xform_id', string='Xmany'),
 }
 _defaults = {
 'xstate': '{}'
 }
 def onchange_many_lines(self, cr, uid, ids, xmany_ids, xstate):
 #to save the state of modified fields to latter could detect new changes
 xstate = json.loads(xstate)
 lines = []
 xlast = False
 #for identify non saved lines
 index = 0
 for xline in xmany_ids:
 if not xlast:
 #detecting the actual modified line
 if xline[0] in (0,1) and xline[2].get('xvalue', False):
 if (xline[0] == 0 and xstate.get('new-%s'%index,False) != xline[2].get('xvalue')) or \
 (xline[0] == 1 and xstate.get(xline[1],False) != xline[2].get('xvalue')):
 xlast = xline[2].get('xvalue')
 if xline[0] == 0:
 xstate['new-%s'%index] = xline[2]
 if xline[0] == 1:
 xstate[xline[1]] = xline[2]
 lines.append(xline)
 print lines,"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
 else:
 #not a deleted line
 if xline[0] != 2:
 xlast+=1
 else:
 lines.append(xline)
 #new line not saved yet
 if xline[0] == 0:
 data = {'xvalue': xlast}
 if xline[2].get('name', False):
 data.update({'name': xline[2]['name']})
 lines.append((0, 0, data))
 xstate['new-%s'%index] = xlast
 #existing lines, 1: modified line and 4: linked line to an existing record
 elif xline[0] in (1,4):
 data = {'xvalue': xlast}
 if xline[0] == 1 and xline[2].get('name', False):
 data.update({'name': xline[2]['name']})
 lines.append((1, xline[1], data))
 xstate[xline[1]] = xlast
 index+=1
 if not xlast:
 print xlast,"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP"
 return {}
 print json.dumps(xlast),"$$$$$$$$$$$$$$$$$$$$$$$$$$$"
 return {'values': {'xstate': json.dumps(xlast), 'xmany_ids': lines}}
xform()
class xmany(osv.osv):
 _name = 'xmany'
 _columns = {
 'name': fields.char('Name', size=64),
 'xvalue': fields.integer('XValue'),
 'xform_id': fields.many2one('xform', string='XForm'),
 }
xmany()
1
a
2
b
3
c
4
d
5
e
15
f
16
g
17
h
18
i
19
j
-2
Avatar
Discard
Axel Mendoza

Describe more what you need, it can be done but you need to be more specific

Avatar
Axel Mendoza
Best Answer

It's something tricky what you need. Here is an example that should solve your problem. It's developed in old Odoo API style. Enjoy it, maybe it's not exactly what you need or need a minor tweaks or validation but it's pretty complete

The models for the example:

from openerp.osv import fields, osv
import json

from openerp import api
from openerp.models import onchange_v7


class xform(osv.osv):
_name = 'xform'
_columns = {
'name': fields.char('Name', size=64),
'xstate': fields.text('XState'),
'xmany_ids': fields.one2many('xmany', 'xform_id', string='Xmany'),
}
_defaults = {
'xstate': '{}'
}

def update_state(self, xform):
xstate = {}
for line in xform.xmany_ids:
xstate[line.id] = line.xvalue
osv.osv.write(xform, {'xstate': json.dumps(xstate)})

@api.model
@api.returns('self', lambda value:value.id)
def create(self, vals):
xform = osv.osv.create(self, vals)
self.update_state(xform)
return xform

@api.multi
def write(self, vals):
res = osv.osv.write(self, vals)
self.update_state(self)
return res

def onchange_many_lines(self, cr, uid, ids, xmany_ids, xstate, **kwargs):
#to save the state of modified fields to latter could detect new changes
xstate = json.loads(xstate)
lines = []
xlast = False
#for identify non saved lines
index = 0

for xline in xmany_ids:
if not xlast:
#detecting the actual modified line
if xline[0] in (0,1) and xline[2].get('xvalue', False):
if (xline[0] == 0 and xstate.get('new-%s'%index,False) != xline[2].get('xvalue')) or \
(xline[0] == 1 and xstate.get(str(xline[1]),False) != xline[2].get('xvalue')):
xlast = xline[2].get('xvalue')
if xline[0] == 0:
xstate['new-%s'%index] = xline[2]['xvalue']
if xline[0] == 1:
xstate[str(xline[1])] = xline[2]['xvalue']
lines.append(xline)
else:
#not a deleted line
if xline[0] != 2:
xlast+=1
else:
lines.append(xline)
#new line not saved yet
if xline[0] == 0:
data = {'xvalue': xlast}
if xline[2].get('name', False):
data.update({'name': xline[2]['name']})
lines.append((0, 0, data))
xstate['new-%s'%index] = xlast
#existing lines, 1: modified line and 4: linked line to an existing record
elif xline[0] in (1,4):
data = {'xvalue': xlast}
if xline[0] == 1 and xline[2].get('name', False):
data.update({'name': xline[2]['name']})
lines.append((1, xline[1], data))
xstate[str(xline[1])] = xlast
index+=1
if not xlast:
return {}
return {'value': {'xstate': json.dumps(xstate), 'xmany_ids': lines}}

@api.multi
def onchange(self, values, field_name, field_onchange):
match = onchange_v7.match(field_onchange[field_name])
if match:
method, params = match.groups()

# evaluate params -> tuple
global_vars = {'context': self._context, 'uid': self._uid}
if self._context.get('field_parent'):
class RawRecord(object):
def __init__(self, record):
self._record = record
def __getattr__(self, name):
field = self._record._fields[name]
value = self._record[name]
return field.convert_to_onchange(value)
record = self[self._context['field_parent']]
global_vars['parent'] = RawRecord(record)
params = eval("[%s]" % params, global_vars, values)

# call onchange method with context when possible
args = (self._cr, self._uid, self._ids) + tuple(params)

try:
method_res = getattr(self._model, method)(*args, context=self._context)
except TypeError:
method_res = getattr(self._model, method)(*args)

return method_res
super(xform,self).onchange(values, field_name, field_onchange)

xform()

class xmany(osv.osv):
_name = 'xmany'
_columns = {
'name': fields.char('Name', size=64),
'xvalue': fields.integer('XValue'),
'xform_id': fields.many2one('xform', string='XForm'),
}
xmany()

in the view for the model xform:

        <record model="ir.ui.view" id="solt_xform_form_view">
<field name="name">xform</field>
<field name="model">xform</field>
<field name="arch" type="xml">
<form string="XFORM">
<field name="name"/>
<field name="xstate" invisible="1"/>
<field name="xmany_ids" on_change="onchange_many_lines(xmany_ids, xstate)">
<tree editable="bottom">
<field name="xvalue"/>
<field name="name"/>
</tree>
</field>
</form>
</field>
</record>

<record id="solt_xform_action" model="ir.actions.act_window">
<field name="name">XFORM</field>
<field name="res_model">xform</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>

<menuitem name="Many Tests" id="solt_many_menu" parent="base.menu_administration" sequence="5"/>
<menuitem name="XFORM" id="solt_xform_submenu" parent="solt_many_menu" action="solt_xform_action" sequence="3"/>

 

4
Avatar
Discard
Axel Mendoza

Change for xlast+=1

sridhar
Author

This concept is not working in ODOO, but it is working in OpenERP 7.

Axel Mendoza

It should work with a few tweaks, Odoo doesn't change too much in that features and I have done already some codes like that way in Odoo v8

Axel Mendoza

I give you a very hard to guess way of solve your problem, creating the models to show you and example you need to adapt to what you need. I think at least a +1 vote it may deserve, try to understand the algorithm used with the tuple format, I left some comments in the code and If you don't get something you could always ask

sridhar
Author

json.dumps(xlast), here it get False only.

Axel Mendoza

I fix the final json.dumps(xstate) because former I code wrong as xtate

sridhar
Author

If I editing xvalue means json.dumps(xstate) is False only.

Axel Mendoza

If you edit xvalue in a line, there is a loop that detect the change in that line. xstate keep the values of all the xvalue lines for be able to detect other changes latter

sridhar
Author

If i edit the lines means, i print the value lines.append(xline) but I got this values only, [(6, 0, [7, 8, 9, 10])],

Axel Mendoza

If you see (6,0,[ids]) then you are dealing with a many2many field or a one2many with a widget many2many in the view

sridhar
Author

No, I'm using your code only, I didn't add any many2many widget in the view.

Axel Mendoza

let me try all latter, come back tomorrow for a solution, the code I give you was not tested at all, but it's simply to add [(6,0,[ids])] support

sridhar
Author

Ok Thank you so much.

sridhar
Author

@Axel Mendoza, Any update to this concept.

Axel Mendoza

I updated the code, it's working in all the scenarios. Please test it and comment if works for you

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
How to update two levels o2m fileds relation
one2many onchange
Avatar
0
Sep 20
3722
Several levels of One2many
one2many onchange
Avatar
0
Apr 16
3842
How to Import one2many field record from one model to one2many field in another model
one2many onchange
Avatar
Avatar
1
Mar 15
13255
How to return domain on one2many field so that seleted values are not shown in next line
domain one2many onchange
Avatar
1
Apr 23
5940
onchange return domain for one2many product ID Field Solved
domain one2many onchange
Avatar
Avatar
Avatar
Avatar
7
Apr 23
19888
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