Skip to Content
Menu
This question has been flagged
5 Replies
1836 Views
class delais(models.Model):

_name = 'delais.delais'
_description = "Delais des projets"
project_ids = fields.Many2one('project.project', string='Projet')
delai_arr_mois = fields.Float(string='Délai d\'Arrét en mois de projet', compute='_delai_arret')

this function calculates the sum of the nbr_mission of each project

def _delai_arret(self):
domaine = ('project_ids', '=', self.project_ids.id)
dict = self.env['mission.mission'].search_read([domaine], ['nbr_mission'])
# print(dict)
if not dict:
pass
else:
somme = 0
for key in dict:
# print(key['nbr_mission'])
somme = somme + key['nbr_mission'] / 30
# print('la somme est : {somme}')
self.delai_arr_mois = somme
# print(somme)

class Missions(models.Model):

_name = 'mission.mission'
_description = 'Mission de chaque projet'

ordre_arre = fields.Date(string='Ordre d\'Arrét')
ordre_repr = fields.Date(string='Ordre de reprise')

nbr_mission = fields.Float(string='nombre de mission', compute='_delais_mission')

this function calculate number of missions

def _delais_mission(self):
for k in self:

if k.ordre_arre and k.ordre_repr:
date_1 = datetime.strptime(k.ordre_arre, '%Y-%m-%d')
date_2 = datetime.strptime(k.ordre_repr, '%Y-%m-%d')
k.nbr_mission = ((date_2 - date_1).days) / 30
#print(k.nbr_mission)

***the error is whene i try to creat a second deadline it gives me error
thank you for finding me a solution

Traceback (most recent call last):
  File "D:\Odoo 11.0\server\odoo\fields.py", line 936, in __get__
    value = record.env.cache.get(record, self)
  File "D:\Odoo 11.0\server\odoo\api.py", line 960, in get
    value = self._data[field][record.id][key]
KeyError: <odoo.api.Environment object at 0x000000000B89EF28>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Odoo 11.0\server\odoo\http.py", line 647, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "D:\Odoo 11.0\server\odoo\http.py", line 307, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "D:\Odoo 11.0\server\odoo\tools\pycompat.py", line 87, in reraise
    raise value
  File "D:\Odoo 11.0\server\odoo\http.py", line 689, in dispatch
    result = self._call_function(**self.params)
  File "D:\Odoo 11.0\server\odoo\http.py", line 339, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "D:\Odoo 11.0\server\odoo\service\model.py", line 97, in wrapper
    return f(dbname, *args, **kwargs)
  File "D:\Odoo 11.0\server\odoo\http.py", line 332, in checked_call
    result = self.endpoint(*a, **kw)
  File "D:\Odoo 11.0\server\odoo\http.py", line 933, in __call__
    return self.method(*args, **kw)
  File "D:\Odoo 11.0\server\odoo\http.py", line 512, in response_wrap
    response = f(*args, **kw)
  File "D:\Odoo 11.0\server\odoo\addons\web\controllers\main.py", line 872, in search_read
    return self.do_search_read(model, fields, offset, limit, domain, sort)
  File "D:\Odoo 11.0\server\odoo\addons\web\controllers\main.py", line 894, in do_search_read
    offset=offset or 0, limit=limit or False, order=sort or False)
  File "D:\Odoo 11.0\server\odoo\models.py", line 4169, in search_read
    result = records.read(fields)
  File "D:\Odoo 11.0\server\odoo\models.py", line 2535, in read
    values[name] = field.convert_to_read(record[name], record, use_name_get)
  File "D:\Odoo 11.0\server\odoo\models.py", line 4688, in __getitem__
    return self._fields[key].__get__(self, type(self))
  File "D:\Odoo 11.0\server\odoo\fields.py", line 940, in __get__
    self.determine_value(record)
  File "D:\Odoo 11.0\server\odoo\fields.py", line 1051, in determine_value
    self.compute_value(recs)
  File "D:\Odoo 11.0\server\odoo\fields.py", line 1007, in compute_value
    self._compute_value(records)
  File "D:\Odoo 11.0\server\odoo\fields.py", line 998, in _compute_value
    getattr(records, self.compute)()
  File "D:\Odoo 11.0\server\odoo\addons\dev\models\delais.py", line 40, in _delai_arret
    def _delai_ecoule_projet(self):
  File "D:\Odoo 11.0\server\odoo\fields.py", line 934, in __get__
    record.ensure_one()
  File "D:\Odoo 11.0\server\odoo\models.py", line 4296, in ensure_one
    raise ValueError("Expected singleton: %s" % self)
ValueError: Expected singleton: delais.delais(2, 18)
 



 
Avatar
Discard
Best Answer

Ravi is right, many2one is many, so if you have two records of projects will be ( 'one', '=', 'one, two') that can't be right

why not :

def _delai_arret(self):
    for rec in self:
    dict = self.env['mission.mission'].search('project_ids', '=', rec.project_ids)
Avatar
Discard
Best Answer

Please send  def _delai_ecoule_projet(self):  function..

Avatar
Discard
Author

def _delai_ecoule_projet(self):

if self.Date_commen and self.delai_arr_mois:

date_3 = datetime.strptime(self.Date_commen, '%Y-%m-%d')

self.delai_ecoule = ((datetime.now()-date_3).days / 30)-self.delai_arr_mois

thank you

Best Answer

Error raise due to   "domaine = ('project_ids', '=', self.project_ids.id)"  Here self has multi recordset and you try to get projects_ids from it

according to your business logic either iterate on self and get new domain for each record or 
use 
domaine = ('project_ids', '=', self.mapped('project_ids').ids)


Avatar
Discard
Author

it remains the same error :(

Related Posts Replies Views Activity
2
Feb 24
13187
1
Dec 22
3859
2
Dec 22
12723
2
Jun 22
4685
2
Jun 22
3504