Ir al contenido
Odoo Menú
  • Identificarse
  • Pruébalo gratis
  • Aplicaciones
    Finanzas
    • Contabilidad
    • Facturación
    • Gastos
    • Hoja de cálculo (BI)
    • Documentos
    • Firma electrónica
    Ventas
    • CRM
    • Ventas
    • TPV para tiendas
    • TPV para restaurantes
    • Suscripciones
    • Alquiler
    Sitios web
    • Creador de sitios web
    • Comercio electrónico
    • Blog
    • Foro
    • Chat en directo
    • eLearning
    Cadena de suministro
    • Inventario
    • Fabricación
    • PLM
    • Compra
    • Mantenimiento
    • Calidad
    Recursos Humanos
    • Empleados
    • Reclutamiento
    • Ausencias
    • Evaluación
    • Referencias
    • Flota
    Marketing
    • Marketing social
    • Marketing por correo electrónico
    • Marketing por SMS
    • Eventos
    • Automatización de marketing
    • Encuestas
    Servicios
    • Proyecto
    • Partes de horas
    • Servicio de campo
    • Servicio de asistencia
    • Planificación
    • Citas
    Productividad
    • Conversaciones
    • Aprobaciones
    • IoT
    • VoIP
    • Información
    • WhatsApp
    Aplicaciones de terceros Studio de Odoo Plataforma de Odoo Cloud
  • Industrias
    Comercio al por menor
    • Librería
    • Tienda de ropa
    • Tienda de muebles
    • Tienda de ultramarinos
    • Ferretería
    • Juguetería
    Food & Hospitality
    • Bar y taberna
    • Restaurante
    • Comida rápida
    • Guest House
    • Distribuidor de bebidas
    • Hotel
    Real Estate
    • Real Estate Agency
    • Estudio de arquitectura
    • Construcción
    • Gestión inmobiliaria
    • Jardinería
    • Asociación de propietarios
    Consulting
    • Accounting Firm
    • Partner de Odoo
    • Agencia de marketing
    • Bufete de abogados
    • Adquisición de talentos
    • Auditorías y certificaciones
    Fabricación
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Regalos de empresas
    Salud y bienestar
    • Club deportivo
    • Óptica
    • Gimnasio
    • Terapeutas
    • Farmacia
    • Peluquería
    Trades
    • Handyman
    • Hardware y asistencia informática
    • Solar Energy Systems
    • Zapatero
    • Servicios de limpieza
    • HVAC Services
    Others
    • Nonprofit Organization
    • Agencia de protección del medio ambiente
    • Alquiler de paneles publicitarios
    • Estudio fotográfico
    • Alquiler de bicicletas
    • Distribuidor de software
    Browse all Industries
  • Comunidad
    Aprender
    • Tutoriales
    • Documentación
    • Certificaciones
    • Formación
    • Blog
    • Podcast
    Potenciar la educación
    • Programa de formación
    • Scale Up! El juego empresarial
    • Visita Odoo
    Obtener el software
    • Descargar
    • Comparar ediciones
    • Versiones
    Colaborar
    • GitHub
    • Foro
    • Eventos
    • Traducciones
    • Convertirse en partner
    • Services for Partners
    • Registrar tu empresa contable
    Obtener servicios
    • Encontrar un partner
    • Encontrar un asesor fiscal
    • Contacta con un experto
    • Servicios de implementación
    • Referencias de clientes
    • Ayuda
    • Actualizaciones
    GitHub YouTube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Solicitar una demostración
  • Precios
  • Ayuda

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

  • CRM
  • e-Commerce
  • Contabilidad
  • Inventario
  • PoS
  • Proyecto
  • MRP
All apps
Debe estar registrado para interactuar con la comunidad.
Todas las publicaciones Personas Insignias
Etiquetas (Ver todo)
odoo accounting v14 pos v15
Acerca de este foro
Debe estar registrado para interactuar con la comunidad.
Todas las publicaciones Personas Insignias
Etiquetas (Ver todo)
odoo accounting v14 pos v15
Acerca de este foro
Ayuda

One2many onchange in ODOO

Suscribirse

Reciba una notificación cuando haya actividad en esta publicación

Se marcó esta pregunta
one2manyonchange
3 Respuestas
14871 Vistas
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
Descartar
Axel Mendoza

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

Avatar
Axel Mendoza
Mejor respuesta

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

Change for xlast+=1

sridhar
Autor

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
Autor

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
Autor

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
Autor

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
Autor

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
Autor

Ok Thank you so much.

sridhar
Autor

@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

¿Le interesa esta conversación? ¡Participe en ella!

Cree una cuenta para poder utilizar funciones exclusivas e interactuar con la comunidad.

Inscribirse
Publicaciones relacionadas Respuestas Vistas Actividad
How to update two levels o2m fileds relation
one2many onchange
Avatar
0
sept 20
3697
Several levels of One2many
one2many onchange
Avatar
0
abr 16
3828
How to Import one2many field record from one model to one2many field in another model
one2many onchange
Avatar
Avatar
1
mar 15
13238
How to return domain on one2many field so that seleted values are not shown in next line
domain one2many onchange
Avatar
1
abr 23
5922
onchange return domain for one2many product ID Field Resuelto
domain one2many onchange
Avatar
Avatar
Avatar
Avatar
7
abr 23
19873
Comunidad
  • Tutoriales
  • Documentación
  • Foro
Código abierto
  • Descargar
  • GitHub
  • Runbot
  • Traducciones
Servicios
  • Alojamiento Odoo.sh
  • Ayuda
  • Actualizar
  • Desarrollos personalizados
  • Educación
  • Encontrar un asesor fiscal
  • Encontrar un partner
  • Convertirse en partner
Sobre nosotros
  • Nuestra empresa
  • Activos de marca
  • Contacta con nosotros
  • Puestos de trabajo
  • Eventos
  • Podcast
  • Blog
  • Clientes
  • Información legal • Privacidad
  • Seguridad
الْعَرَبيّة 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 es un conjunto de aplicaciones de código abierto que cubren todas las necesidades de tu empresa: CRM, comercio electrónico, contabilidad, inventario, punto de venta, gestión de proyectos, etc.

La propuesta única de valor de Odoo es ser muy fácil de usar y totalmente integrado.

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