Skip to Content
Menu
This question has been flagged
4 Replies
2239 Views

Hello Odoo Community,

In the Sales module, I am trying to display in a field the week number of the Commitment Date.  

I tried to create an x_week field by making a dependency with the commitment date but I don't success with the calculation formula. 

Do you have an idea ? 

Thank you ! 

Avatar
Discard
Author

Here is the solution :
for rec in self:
if rec.commitment_date:
rec['x_semaine'] = rec.commitment_date.isocalendar()[1]
else:
rec['x_semaine'] = 0

Best Answer
commitment_date.strftime("%YW%W")


Avatar
Discard
Best Answer

Hi Louis Cordelette,

Please add the Integer type function field for the week number and use the isocalendar python library for find the week number.


@api.depends('Commitment Date')
def _check_week_number(self):
for rec in self:
rec.commitment_date_week_no = int(your Commitment Date.isocalendar()[1])

Hope it will help you.



Edited Ans:

Field Definition
commitment_date_week_no = fields.Integer(compute="_check_week_number", string="Week Number")


Method Definition
@api.depends('commitment_date')
def _check_week_number(self):
for rec in self:
date_week_no = 0
if rec.commitment_date:
date_week_no = int(rec.commitment_date.isocalendar()[1])
rec.commitment_date_week_no = date_week_no


Avatar
Discard
Author Best Answer

Hello,

Thanks for your quick answers but for the two solutions I have errors. 

For Malay solution :

Traceback (most recent call last):
  File "/home/odoo/src/odoo/odoo/http.py", line 1578, in _serve_db
    return service_model.retrying(self._serve_ir_http, self.env)
  File "/home/odoo/src/odoo/odoo/service/model.py", line 134, in retrying
    result = func()
  File "/home/odoo/src/odoo/odoo/http.py", line 1607, in _serve_ir_http
    response = self.dispatcher.dispatch(rule.endpoint, args)
  File "/home/odoo/src/odoo/odoo/http.py", line 1804, in dispatch
    result = self.request.registry['ir.http']._dispatch(endpoint)
  File "/home/odoo/src/odoo/odoo/addons/base/models/ir_http.py", line 140, in _dispatch
    result = endpoint(**request.params)
  File "/home/odoo/src/odoo/odoo/http.py", line 698, in route_wrapper
    result = endpoint(self, *args, **params_ok)
  File "/home/odoo/src/odoo/addons/web/controllers/dataset.py", line 42, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/home/odoo/src/odoo/addons/web/controllers/dataset.py", line 33, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/home/odoo/src/odoo/odoo/api.py", line 459, in call_kw
    result = _call_kw_model_create(method, model, args, kwargs)
  File "/home/odoo/src/odoo/odoo/api.py", line 439, in _call_kw_model_create
    result = method(recs, *args, **kwargs)
  File "", line 2, in create
  File "/home/odoo/src/odoo/odoo/api.py", line 409, in _model_create_multi
    return create(self, [arg])
  File "/home/odoo/src/odoo/odoo/addons/base/models/ir_model.py", line 915, in create
    self.pool.init_models(self._cr, models, dict(self._context, update_custom_fields=True))
  File "/home/odoo/src/odoo/odoo/modules/registry.py", line 465, in init_models
    func()
  File "/home/odoo/src/odoo/odoo/addons/base/models/ir_model.py", line 1720, in _reflect_relation
    self.env.invalidate_all()
  File "/home/odoo/src/odoo/odoo/api.py", line 722, in invalidate_all
    self.flush_all()
  File "/home/odoo/src/odoo/odoo/api.py", line 732, in flush_all
    self._recompute_all()
  File "/home/odoo/src/odoo/odoo/api.py", line 728, in _recompute_all
    self[field.model_name]._recompute_field(field)
  File "/home/odoo/src/odoo/odoo/models.py", line 6155, in _recompute_field
    field.recompute(records)
  File "/home/odoo/src/odoo/odoo/fields.py", line 1324, in recompute
    self.compute_value(recs)
  File "/home/odoo/src/odoo/odoo/fields.py", line 1346, in compute_value
    records._compute_field_value(self)
  File "/home/odoo/src/odoo/addons/sale/models/sale_order.py", line 1357, in _compute_field_value
    return super()._compute_field_value(field)
  File "/home/odoo/src/odoo/addons/mail/models/mail_thread.py", line 403, in _compute_field_value
    return super()._compute_field_value(field)
  File "/home/odoo/src/odoo/odoo/models.py", line 4190, in _compute_field_value
    field.compute(self)
  File "/home/odoo/src/odoo/odoo/addons/base/models/ir_model.py", line 37, in 
    func = lambda self: safe_eval(text, SAFE_EVAL_BASE, {'self': self}, mode="exec")
  File "/home/odoo/src/odoo/odoo/tools/safe_eval.py", line 337, in safe_eval
    c = test_expr(expr, _SAFE_OPCODES, mode=mode, filename=filename)
  File "/home/odoo/src/odoo/odoo/tools/safe_eval.py", line 190, in test_expr
    code_obj = compile(expr, filename or "", mode)
  File "", line 4
    rec.commitment_date_week_no = int(your Commitment Date.isocalendar()[1])
                                      ^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

For Abhishek soluton : 

Traceback (most recent call last):
  File "/home/odoo/src/odoo/odoo/http.py", line 1578, in _serve_db
    return service_model.retrying(self._serve_ir_http, self.env)
  File "/home/odoo/src/odoo/odoo/service/model.py", line 134, in retrying
    result = func()
  File "/home/odoo/src/odoo/odoo/http.py", line 1607, in _serve_ir_http
    response = self.dispatcher.dispatch(rule.endpoint, args)
  File "/home/odoo/src/odoo/odoo/http.py", line 1804, in dispatch
    result = self.request.registry['ir.http']._dispatch(endpoint)
  File "/home/odoo/src/odoo/odoo/addons/base/models/ir_http.py", line 140, in _dispatch
    result = endpoint(**request.params)
  File "/home/odoo/src/odoo/odoo/http.py", line 698, in route_wrapper
    result = endpoint(self, *args, **params_ok)
  File "/home/odoo/src/odoo/addons/web/controllers/dataset.py", line 42, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/home/odoo/src/odoo/addons/web/controllers/dataset.py", line 33, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/home/odoo/src/odoo/odoo/api.py", line 459, in call_kw
    result = _call_kw_model_create(method, model, args, kwargs)
  File "/home/odoo/src/odoo/odoo/api.py", line 439, in _call_kw_model_create
    result = method(recs, *args, **kwargs)
  File "", line 2, in create
  File "/home/odoo/src/odoo/odoo/api.py", line 409, in _model_create_multi
    return create(self, [arg])
  File "/home/odoo/src/odoo/odoo/addons/base/models/ir_model.py", line 915, in create
    self.pool.init_models(self._cr, models, dict(self._context, update_custom_fields=True))
  File "/home/odoo/src/odoo/odoo/modules/registry.py", line 465, in init_models
    func()
  File "/home/odoo/src/odoo/odoo/addons/base/models/ir_model.py", line 1720, in _reflect_relation
    self.env.invalidate_all()
  File "/home/odoo/src/odoo/odoo/api.py", line 722, in invalidate_all
    self.flush_all()
  File "/home/odoo/src/odoo/odoo/api.py", line 732, in flush_all
    self._recompute_all()
  File "/home/odoo/src/odoo/odoo/api.py", line 728, in _recompute_all
    self[field.model_name]._recompute_field(field)
  File "/home/odoo/src/odoo/odoo/models.py", line 6155, in _recompute_field
    field.recompute(records)
  File "/home/odoo/src/odoo/odoo/fields.py", line 1324, in recompute
    self.compute_value(recs)
  File "/home/odoo/src/odoo/odoo/fields.py", line 1346, in compute_value
    records._compute_field_value(self)
  File "/home/odoo/src/odoo/addons/sale/models/sale_order.py", line 1357, in _compute_field_value
    return super()._compute_field_value(field)
  File "/home/odoo/src/odoo/addons/mail/models/mail_thread.py", line 403, in _compute_field_value
    return super()._compute_field_value(field)
  File "/home/odoo/src/odoo/odoo/models.py", line 4190, in _compute_field_value
    field.compute(self)
  File "/home/odoo/src/odoo/odoo/addons/base/models/ir_model.py", line 37, in 
    func = lambda self: safe_eval(text, SAFE_EVAL_BASE, {'self': self}, mode="exec")
  File "/home/odoo/src/odoo/odoo/tools/safe_eval.py", line 337, in safe_eval
    c = test_expr(expr, _SAFE_OPCODES, mode=mode, filename=filename)
  File "/home/odoo/src/odoo/odoo/tools/safe_eval.py", line 195, in test_expr
    assert_valid_codeobj(allowed_codes, code_obj, expr)
  File "/home/odoo/src/odoo/odoo/tools/safe_eval.py", line 168, in assert_valid_codeobj
    raise ValueError("forbidden opcode(s) in %r: %s" % (expr, ', '.join(opname[x] for x in (code_codes - allowed_codes))))
ValueError: forbidden opcode(s) in 'import datetime\ndatetime.date(2010, 6, 16).isocalendar()[1]': IMPORT_NAME
Avatar
Discard

@api.onchange('commitment_date')
def _onchange_commitment_date(self):
if self.commitment_date:
self.week_number = int(self.commitment_date.isocalendar()[1])

Try this bro!!

Author

Abhishek, it doesn't work either...

Traceback (most recent call last):
File "/home/odoo/src/odoo/odoo/api.py", line 983, in get
cache_value = field_cache[record._ids[0]]
KeyError: <NewId 0x7fa0485f1cc0>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/odoo/src/odoo/odoo/fields.py", line 1138, in __get__
value = env.cache.get(record, self)
File "/home/odoo/src/odoo/odoo/api.py", line 990, in get
raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: 'sale.order(<NewId 0x7fa0485f1cc0>,).x_semaine'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/odoo/src/odoo/odoo/http.py", line 1578, in _serve_db
return service_model.retrying(self._serve_ir_http, self.env)
File "/home/odoo/src/odoo/odoo/service/model.py", line 134, in retrying
result = func()
File "/home/odoo/src/odoo/odoo/http.py", line 1607, in _serve_ir_http
response = self.dispatcher.dispatch(rule.endpoint, args)
File "/home/odoo/src/odoo/odoo/http.py", line 1804, in dispatch
result = self.request.registry['ir.http']._dispatch(endpoint)
File "/home/odoo/src/odoo/odoo/addons/base/models/ir_http.py", line 140, in _dispatch
result = endpoint(**request.params)
File "/home/odoo/src/odoo/odoo/http.py", line 698, in route_wrapper
result = endpoint(self, *args, **params_ok)
File "/home/odoo/src/odoo/addons/web/controllers/dataset.py", line 42, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/home/odoo/src/odoo/addons/web/controllers/dataset.py", line 33, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/home/odoo/src/odoo/odoo/api.py", line 461, in call_kw
result = _call_kw_multi(method, model, args, kwargs)
File "/home/odoo/src/odoo/odoo/api.py", line 448, in _call_kw_multi
result = method(recs, *args, **kwargs)
File "/home/odoo/src/odoo/odoo/models.py", line 6522, in onchange
snapshot1 = Snapshot(record, nametree)
File "/home/odoo/src/odoo/odoo/models.py", line 6282, in __init__
self.fetch(name)
File "/home/odoo/src/odoo/odoo/models.py", line 6292, in fetch
self[name] = record[name]
File "/home/odoo/src/odoo/odoo/models.py", line 5843, in __getitem__
return self._fields[key].__get__(self, type(self))
File "/home/odoo/src/odoo/odoo/fields.py", line 1187, in __get__
self.compute_value(recs)
File "/home/odoo/src/odoo/odoo/fields.py", line 1346, in compute_value
records._compute_field_value(self)
File "/home/odoo/src/odoo/addons/sale/models/sale_order.py", line 1357, in _compute_field_value
return super()._compute_field_value(field)
File "/home/odoo/src/odoo/addons/mail/models/mail_thread.py", line 403, in _compute_field_value
return super()._compute_field_value(field)
File "/home/odoo/src/odoo/odoo/models.py", line 4190, in _compute_field_value
field.compute(self)
File "/home/odoo/src/odoo/odoo/addons/base/models/ir_model.py", line 37, in <lambda>
func = lambda self: safe_eval(text, SAFE_EVAL_BASE, {'self': self}, mode="exec")
File "/home/odoo/src/odoo/odoo/tools/safe_eval.py", line 337, in safe_eval
c = test_expr(expr, _SAFE_OPCODES, mode=mode, filename=filename)
File "/home/odoo/src/odoo/odoo/tools/safe_eval.py", line 190, in test_expr
code_obj = compile(expr, filename or "", mode)
File "<string>", line 3
if self.commitment_date:
^^
IndentationError: expected an indented block after function definition on line 2

Hello Louis Cordelette, Please correct the indentation of code it will work.

@api.onchange('commitment_date')
def _onchange_commitment_date(self):
if self.commitment_date:
self.commitment_date_week_no = int(self.commitment_date.isocalendar()[1])

Hi Louis Cordelette,
Please try this accurate code

Field Definition
commitment_date_week_no = fields.Integer(compute="_check_week_number", string="Week Number")

Method Definition
@api.depends('commitment_date')
def _check_week_number(self):
for rec in self:
date_week_no = 0
if rec.commitment_date:
date_week_no = int(rec.commitment_date.isocalendar()[1])
rec.commitment_date_week_no = date_week_no

Best Answer
import datetime
datetime.date(2010, 6, 16).isocalendar()[1]


Have you tried using this, it worked for me.


Avatar
Discard
Related Posts Replies Views Activity
1
Dec 24
31
1
Dec 24
77
0
Nov 24
53
0
Nov 24
68
1
Oct 24
171