Skip to Content
Menu
This question has been flagged
2 Replies
3349 Views

Hi, 

I need a help :

I want to compute : poids_moyen = poid_total / nombre

Code .py

from odoo import models, fields, api

class ProductionHomogeneite(models.Model):
  _name = 'production.homogeneite'
  _description ="L'homogénéité"
  _inherit = ['mail.thread']

  name = fields.Date(string='Date', required=True, track_visibility='always')
  age = fields.Integer(string='Age (Jours)', required=True, track_visibility='onchange')
  nombre = fields.Integer(string='Nombre de poussin', required=True, track_visibility='onchange')
  poids_total = fields.Float(string='Poids total (kg)', required=True, track_visibility='onchange')
  poids_moyen = fields.Float(string='Poids moyen (kg/poussin)', readonly=True, compute='onchange_poids_moyen', track_visibility='onchange')
  cycle_id = fields.Many2one('production.cycle', string='Cycle', ondelete='cascade', readonly=True, track_visibility='onchange')
  
  _sql_constraints = [
('name_uniq', 'unique (name, age, cycle_id)', "L'homogénéité existe déjà !"),
]

  @ api.onchange ( 'nombre', 'poids_total' )
  def onchange_poids_moyen ( self ):
    if self.nombre or self.poids_total:
      self .poids_moyen = self.poids_total / self.nombre


But  I found this error :


Erreur:

Odoo Server Error


Traceback (most recent call last):

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 1039, in get

    value = self._data[key][field][record.id]

KeyError: 1


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 963, in __get__

    value = record.env.cache.get(record, self)

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 1041, in get

    raise CacheMiss(record, field)

odoo.exceptions.CacheMiss: ('production.homogeneite(1,).poids_moyen', None)


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 654, in _handle_exception

    return super(JsonRequest, self)._handle_exception(exception)

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 312, in _handle_exception

    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\pycompat.py", line 87, in reraise

    raise value

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 696, in dispatch

    result = self._call_function(**self.params)

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 344, in _call_function

    return checked_call(self.db, *args, **kwargs)

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\service\model.py", line 97, in wrapper

    return f(dbname, *args, **kwargs)

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 337, in checked_call

    result = self.endpoint(*a, **kw)

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 939, in __call__

    return self.method(*args, **kw)

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 517, in response_wrap

    response = f(*args, **kw)

  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\web\controllers\main.py", line 904, in search_read

    return self.do_search_read(model, fields, offset, limit, domain, sort)

  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\web\controllers\main.py", line 926, in do_search_read

    offset=offset or 0, limit=limit or False, order=sort or False)

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 4542, in search_read

    result = records.read(fields)

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 2780, in read

    data[record][name] = convert(record[name], record, use_name_get)

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 5065, in __getitem__

    return self._fields[key].__get__(self, type(self))

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 967, in __get__

    self.determine_value(record)

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 1078, in determine_value

    self.compute_value(recs)

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 1034, in compute_value

    self._compute_value(records)

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 1025, in _compute_value

    getattr(records, self.compute)()

  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\production\models\homogeneite.py", line 29, in onchange_poids_moyen

    if self.nombre or self.poids_total:

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 961, in __get__

    record.ensure_one()

  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 4669, in ensure_one

    raise ValueError("Expected singleton: %s" % self)

ValueError: Expected singleton: production.homogeneite(1, 2)

Avatar
Discard
Best Answer

Hi,

Update your onchange function like this,

 
@api.onchange('nombre', 'poids_total')
def onchange_poids_moyen(self):
for rec in self:
if rec.nombre and rec.poids_total:
rec.poids_moyen = rec.poids_total / rec.nombre


Thanks

Avatar
Discard
Author

Erreur:

Odoo Server Error

Traceback (most recent call last):

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 757, in parse

self._tags[rec.tag](rec, de, mode=mode)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 662, in _tag_record

record = model.with_context(rec_context)._load_records([data], self.mode == 'update')

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3794, in _load_records

data['record']._load_records_write(data['values'])

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3732, in _load_records_write

self.write(values)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_actions.py", line 66, in write

res = super(IrActions, self).write(vals)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3269, in write

self._write(store_vals)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3413, in _write

self._validate_fields(vals)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 1108, in _validate_fields

check(self)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_actions.py", line 141, in _check_model

raise ValidationError(_('Invalid model name %r in action definition.') % action.res_model)

odoo.exceptions.ValidationError: ("Nom de modèle 'production.homogeneite' non valide dans la définition de l'action.", None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 654, in _handle_exception

return super(JsonRequest, self)._handle_exception(exception)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 312, in _handle_exception

raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\pycompat.py", line 87, in reraise

raise value

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 696, in dispatch

result = self._call_function(**self.params)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 344, in _call_function

return checked_call(self.db, *args, **kwargs)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\service\model.py", line 97, in wrapper

return f(dbname, *args, **kwargs)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 337, in checked_call

result = self.endpoint(*a, **kw)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 939, in __call__

return self.method(*args, **kw)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 517, in response_wrap

response = f(*args, **kw)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\web\controllers\main.py", line 966, in call_button

action = self._call_kw(model, method, args, {})

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\web\controllers\main.py", line 954, in _call_kw

return call_kw(request.env[model], method, args, kwargs)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 749, in call_kw

return _call_kw_multi(method, model, args, kwargs)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 736, in _call_kw_multi

result = method(recs, *args, **kwargs)

File "<decorator-gen-67>", line 2, in button_immediate_upgrade

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 73, in check_and_log

return method(self, *args, **kwargs)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 614, in button_immediate_upgrade

return self._button_immediate_function(type(self).button_upgrade)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 553, in _button_immediate_function

modules.registry.Registry.new(self._cr.dbname, update_module=True)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\registry.py", line 86, in new

odoo.modules.load_modules(registry._db, force_demo, status, update_module)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 417, in load_modules

force, status, report, loaded_modules, update_module, models_to_check)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 313, in load_marked_modules

perform_checks=perform_checks, models_to_check=models_to_check

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 222, in load_module_graph

load_data(cr, idref, mode, kind='data', package=package, report=report)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 68, in load_data

tools.convert_file(cr, package.name, filename, idref, mode, noupdate, kind, report)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 801, in convert_file

convert_xml_import(cr, module, fp, idref, mode, noupdate, report)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 864, in convert_xml_import

obj.parse(doc.getroot(), mode=mode)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 754, in parse

self.parse(rec, mode)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 763, in parse

exc_info[2]

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\pycompat.py", line 86, in reraise

raise value.with_traceback(tb)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 757, in parse

self._tags[rec.tag](rec, de, mode=mode)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 662, in _tag_record

record = model.with_context(rec_context)._load_records([data], self.mode == 'update')

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3794, in _load_records

data['record']._load_records_write(data['values'])

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3732, in _load_records_write

self.write(values)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_actions.py", line 66, in write

res = super(IrActions, self).write(vals)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3269, in write

self._write(store_vals)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3413, in _write

self._validate_fields(vals)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 1108, in _validate_fields

check(self)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_actions.py", line 141, in _check_model

raise ValidationError(_('Invalid model name %r in action definition.') % action.res_model)

odoo.tools.convert.ParseError: "Nom de modèle 'production.homogeneite' non valide dans la définition de l'action.

None" while parsing file:/c:/program files (x86)/odoo 12.0/server/odoo/addons/production/views/cycle_views.xml:190, near

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

<field name="name">Homogénéités</field>

<field name="context">{'default_cycle_id': active_id}</field>

<field name="domain">[('cycle_id', '=', active_id)]</field>

<field name="res_model">production.homogeneite</field>

</record>

Best Answer
try this
  @ api.one 
  @ api.depends ( 'nombre', 'poids_total' )
  def onchange_poids_moyen ( self ):
    if self.nombre and self.poids_total:
      self .poids_moyen = self.poids_total / self.nombre
    else:
      self.poids_moyen = False
Avatar
Discard
Author

Erreur:

Odoo Server Error

Traceback (most recent call last):

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 757, in parse

self._tags[rec.tag](rec, de, mode=mode)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 662, in _tag_record

record = model.with_context(rec_context)._load_records([data], self.mode == 'update')

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3794, in _load_records

data['record']._load_records_write(data['values'])

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3732, in _load_records_write

self.write(values)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_actions.py", line 66, in write

res = super(IrActions, self).write(vals)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3269, in write

self._write(store_vals)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3413, in _write

self._validate_fields(vals)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 1108, in _validate_fields

check(self)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_actions.py", line 141, in _check_model

raise ValidationError(_('Invalid model name %r in action definition.') % action.res_model)

odoo.exceptions.ValidationError: ("Nom de modèle 'production.homogeneite' non valide dans la définition de l'action.", None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 654, in _handle_exception

return super(JsonRequest, self)._handle_exception(exception)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 312, in _handle_exception

raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\pycompat.py", line 87, in reraise

raise value

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 696, in dispatch

result = self._call_function(**self.params)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 344, in _call_function

return checked_call(self.db, *args, **kwargs)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\service\model.py", line 97, in wrapper

return f(dbname, *args, **kwargs)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 337, in checked_call

result = self.endpoint(*a, **kw)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 939, in __call__

return self.method(*args, **kw)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 517, in response_wrap

response = f(*args, **kw)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\web\controllers\main.py", line 966, in call_button

action = self._call_kw(model, method, args, {})

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\web\controllers\main.py", line 954, in _call_kw

return call_kw(request.env[model], method, args, kwargs)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 749, in call_kw

return _call_kw_multi(method, model, args, kwargs)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 736, in _call_kw_multi

result = method(recs, *args, **kwargs)

File "<decorator-gen-67>", line 2, in button_immediate_upgrade

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 73, in check_and_log

return method(self, *args, **kwargs)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 614, in button_immediate_upgrade

return self._button_immediate_function(type(self).button_upgrade)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_module.py", line 553, in _button_immediate_function

modules.registry.Registry.new(self._cr.dbname, update_module=True)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\registry.py", line 86, in new

odoo.modules.load_modules(registry._db, force_demo, status, update_module)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 417, in load_modules

force, status, report, loaded_modules, update_module, models_to_check)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 313, in load_marked_modules

perform_checks=perform_checks, models_to_check=models_to_check

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 222, in load_module_graph

load_data(cr, idref, mode, kind='data', package=package, report=report)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\modules\loading.py", line 68, in load_data

tools.convert_file(cr, package.name, filename, idref, mode, noupdate, kind, report)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 801, in convert_file

convert_xml_import(cr, module, fp, idref, mode, noupdate, report)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 864, in convert_xml_import

obj.parse(doc.getroot(), mode=mode)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 754, in parse

self.parse(rec, mode)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 763, in parse

exc_info[2]

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\pycompat.py", line 86, in reraise

raise value.with_traceback(tb)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 757, in parse

self._tags[rec.tag](rec, de, mode=mode)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\convert.py", line 662, in _tag_record

record = model.with_context(rec_context)._load_records([data], self.mode == 'update')

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3794, in _load_records

data['record']._load_records_write(data['values'])

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3732, in _load_records_write

self.write(values)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_actions.py", line 66, in write

res = super(IrActions, self).write(vals)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3269, in write

self._write(store_vals)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 3413, in _write

self._validate_fields(vals)

File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 1108, in _validate_fields

check(self)

File "c:\program files (x86)\odoo 12.0\server\odoo\addons\base\models\ir_actions.py", line 141, in _check_model

raise ValidationError(_('Invalid model name %r in action definition.') % action.res_model)

odoo.tools.convert.ParseError: "Nom de modèle 'production.homogeneite' non valide dans la définition de l'action.

None" while parsing file:/c:/program files (x86)/odoo 12.0/server/odoo/addons/production/views/cycle_views.xml:190, near

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

<field name="name">Homogénéités</field>

<field name="context">{'default_cycle_id': active_id}</field>

<field name="domain">[('cycle_id', '=', active_id)]</field>

<field name="res_model">production.homogeneite</field>

</record>

Related Posts Replies Views Activity
4
May 19
3805
2
May 19
2835
1
Apr 19
4187
0
Apr 19
2868
1
Apr 19
8185