Skip ke Konten
Odoo Menu
  • Login
  • Uji coba gratis
  • Aplikasi
    Keuangan
    • Akuntansi
    • Faktur
    • Pengeluaran
    • Spreadsheet (BI)
    • Dokumen
    • Tanda Tangan
    Sales
    • CRM
    • Sales
    • POS Toko
    • POS Restoran
    • Langganan
    • Rental
    Website
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Rantai Pasokan
    • Inventaris
    • Manufaktur
    • PLM
    • Purchase
    • Maintenance
    • Kualitas
    Sumber Daya Manusia
    • Karyawan
    • Rekrutmen
    • Cuti
    • Appraisal
    • Referensi
    • Armada
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Acara
    • Otomatisasi Marketing
    • Survei
    Layanan
    • Project
    • Timesheet
    • Layanan Lapangan
    • Meja Bantuan
    • Planning
    • Appointment
    Produktivitas
    • Diskusi
    • Approval
    • IoT
    • VoIP
    • Pengetahuan
    • WhatsApp
    Aplikasi pihak ketiga Odoo Studio Platform Odoo Cloud
  • Industri-Industri
    Retail
    • Toko Buku
    • Toko Baju
    • Toko Furnitur
    • Toko Kelontong
    • Toko Hardware
    • Toko Mainan
    Makanan & Hospitality
    • Bar dan Pub
    • Restoran
    • Fast Food
    • Rumah Tamu
    • Distributor Minuman
    • Hotel
    Real Estate
    • Agensi Real Estate
    • Firma Arsitektur
    • Konstruksi
    • Estate Management
    • Perkebunan
    • Asosiasi Pemilik Properti
    Konsultansi
    • Firma Akuntansi
    • Mitra Odoo
    • Agensi Marketing
    • Firma huku
    • Talent Acquisition
    • Audit & Sertifikasi
    Manufaktur
    • Tekstil
    • Logam
    • Perabotan
    • Makanan
    • Brewery
    • Corporate Gift
    Kesehatan & Fitness
    • Sports Club
    • Toko Kacamata
    • Fitness Center
    • Wellness Practitioners
    • Farmasi
    • Salon Rambut
    Perdagangan
    • Handyman
    • IT Hardware & Support
    • Sistem-Sistem Energi Surya
    • Pembuat Sepatu
    • Cleaning Service
    • Layanan HVAC
    Lainnya
    • Organisasi Nirlaba
    • Agen Lingkungan
    • Rental Billboard
    • Fotografi
    • Penyewaan Sepeda
    • Reseller Software
    Browse semua Industri
  • Komunitas
    Belajar
    • Tutorial-tutorial
    • Dokumentasi
    • Sertifikasi
    • Pelatihan
    • Blog
    • Podcast
    Empower Education
    • Program Edukasi
    • Game Bisnis 'Scale Up!'
    • Kunjungi Odoo
    Dapatkan Softwarenya
    • Download
    • Bandingkan Edisi
    • Daftar Rilis
    Kolaborasi
    • Github
    • Forum
    • Acara
    • Terjemahan
    • Menjadi Partner
    • Layanan untuk Partner
    • Daftarkan perusahaan Akuntansi Anda.
    Dapatkan Layanan
    • Temukan Mitra
    • Temukan Akuntan
    • Temui penasihat
    • Layanan Implementasi
    • Referensi Pelanggan
    • Bantuan
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dapatkan demo
  • Harga
  • Bantuan

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

  • CRM
  • e-Commerce
  • Akuntansi
  • Inventaris
  • PoS
  • Project
  • MRP
All apps
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Anda harus terdaftar untuk dapat berinteraksi di komunitas.
Semua Post Orang Lencana-Lencana
Label (Lihat semua)
odoo accounting v14 pos v15
Mengenai forum ini
Help

One2many onchange in ODOO

Langganan

Dapatkan notifikasi saat terdapat aktivitas pada post ini

Pertanyaan ini telah diberikan tanda
one2manyonchange
3 Replies
14875 Tampilan
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
Buang
Axel Mendoza

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

Avatar
Axel Mendoza
Jawaban Terbai

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
Buang
Axel Mendoza

Change for xlast+=1

sridhar
Penulis

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
Penulis

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
Penulis

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
Penulis

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
Penulis

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
Penulis

Ok Thank you so much.

sridhar
Penulis

@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

Menikmati diskusi? Jangan hanya membaca, ikuti!

Buat akun sekarang untuk menikmati fitur eksklufi dan agar terlibat dengan komunitas kami!

Daftar
Post Terkait Replies Tampilan Aktivitas
How to update two levels o2m fileds relation
one2many onchange
Avatar
0
Sep 20
3703
Several levels of One2many
one2many onchange
Avatar
0
Apr 16
3832
How to Import one2many field record from one model to one2many field in another model
one2many onchange
Avatar
Avatar
1
Mar 15
13240
How to return domain on one2many field so that seleted values are not shown in next line
domain one2many onchange
Avatar
1
Apr 23
5927
onchange return domain for one2many product ID Field Diselesaikan
domain one2many onchange
Avatar
Avatar
Avatar
Avatar
7
Apr 23
19875
Komunitas
  • Tutorial-tutorial
  • Dokumentasi
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Terjemahan
Layanan
  • Odoo.sh Hosting
  • Bantuan
  • Peningkatan
  • Custom Development
  • Pendidikan
  • Temukan Akuntan
  • Temukan Mitra
  • Menjadi Partner
Tentang Kami
  • Perusahaan kami
  • Aset Merek
  • Hubungi kami
  • Tugas
  • Acara
  • Podcast
  • Blog
  • Pelanggan
  • Hukum • Privasi
  • Keamanan
الْعَرَبيّة 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 adalah rangkaian aplikasi bisnis open source yang mencakup semua kebutuhan perusahaan Anda: CRM, eCommerce, akuntansi, inventaris, point of sale, manajemen project, dan seterusnya.

Mudah digunakan dan terintegrasi penuh pada saat yang sama adalah value proposition unik Odoo.

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