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

I have a ledger_id many2one and a close_date field. I need to set the default value of the close_date = ledger_id.next_close_date.

I'm working on something like:

ledger_id = fields.Many2one('contribution.ledger', string='Contribution Ledger', default=_get_default_ledger_id, readonly=True)
close_date = fields.Date(string='Close Date', default='_default_close_date')

@api.depends('ledger_id')
def _default_close_date(self):
for record in self:
return record.ledger_id.next_close_date if record.ledger_id.next_close_date else date.today()

But it shows error message: 

Traceback (most recent call last):
  File "/Users/giangnguyen/odoo/odoo/http.py", line 1584, in _serve_db
    return service_model.retrying(self._serve_ir_http, self.env)
  File "/Users/giangnguyen/odoo/odoo/service/model.py", line 134, in retrying
    result = func()
  File "/Users/giangnguyen/odoo/odoo/http.py", line 1613, in _serve_ir_http
    response = self.dispatcher.dispatch(rule.endpoint, args)
  File "/Users/giangnguyen/odoo/odoo/http.py", line 1810, in dispatch
    result = self.request.registry['ir.http']._dispatch(endpoint)
  File "/Users/giangnguyen/odoo/odoo/addons/base/models/ir_http.py", line 149, in _dispatch
    result = endpoint(**request.params)
  File "/Users/giangnguyen/odoo/odoo/http.py", line 699, in route_wrapper
    result = endpoint(self, *args, **params_ok)
  File "/Users/giangnguyen/odoo/addons/web/controllers/dataset.py", line 42, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/Users/giangnguyen/odoo/addons/web/controllers/dataset.py", line 33, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/Users/giangnguyen/odoo/odoo/api.py", line 461, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
  File "/Users/giangnguyen/odoo/odoo/api.py", line 448, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "/Users/giangnguyen/odoo/odoo/models.py", line 6371, in onchange
    defaults = self.default_get(missing_names)
  File "/Users/giangnguyen/odoo/odoo/models.py", line 1407, in default_get
    value = field.convert_to_cache(value, self, validate=False)
  File "/Users/giangnguyen/odoo/odoo/fields.py", line 2118, in convert_to_cache
    return self.to_date(value)
  File "/Users/giangnguyen/odoo/odoo/fields.py", line 2093, in to_date
    return datetime.strptime(value, DATE_FORMAT).date()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '_default_c' does not match format '%Y-%m-%d'

The above server error caused the following client error:
null

Please help. Thank you.

Avatar
Discard
Best Answer

Hi,

Change the default attribute of the close_date field to the name of the method _default_close_date (without quotes) instead of the string '_default_close_date'.

close_date = fields.Date(string = 'Close Date', default = _default_close_date)

Regards

Avatar
Discard
Author Best Answer

Thank you very much. I changed the attribute to the name of the method and it works now.

But I need to move the _default_close_date to the top and change the logic as well. I'm not sure if my approach is correct or not.

def _default_close_date(self):
ledger_id = False
if self
.env.context.get('active_id'):
ledger_id = self.env['contribution.ledger'].browse(self.env.context['active_id'])
return ledger_id.next_close_date if ledger_id.next_close_date else date.today()

Avatar
Discard