0

I am new to OpenERP and Python and I am trying to set a default date in a create form which has to be 28 days after when the user is using the create form.

The last thing I've tried is this :

from datetime import datetime
from datetime import date
from datetime import timedelta
from dateutil.relativedelta import relativedelta

from openerp.osv import fields, osv


class sale_order_dates(osv.osv):
    _inherit = 'sale.order'

    _columns = {
        'requested_date': fields.date('Requested Date', help="Date requested by the customer for the sale."),
    }

    _defaults = {
        'requested_date': date.today() + timedelta(days=28),
    }

sale_order_dates()

But then if I open the create form I get this error :

"The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application."

It is probably because I can't make operations in _defaults but then I don't know what to do, I've tried to make the operation in a function but I am not very comfortable with functions yet. Do you have any ideas of how I could do it please ? Thanks in advance

Edit : This is the error message on the computer terminal

2015-04-30 19:50:40,217 8666 ERROR Armand werkzeug: Error on request:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 180, in run_wsgi
execute(self.server.app)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 168, in execute 
application_iter = app(environ, start_response)
File "/home/odoo/server/7.0/openerp/service/wsgi_server.py", line 417, in application
return application_unproxied(environ, start_response)
File "/home/odoo/server/7.0/openerp/service/wsgi_server.py", line 403, in application_unproxied
result = handler(environ, start_response)
File "/home/odoo/web/7.0/addons/web/http.py", line 528, in __call__
return self.dispatch(environ, start_response)
File "/home/odoo/web/7.0/addons/web/http.py", line 487, in __call__
return self.app(environ, start_wrapped)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 591, in __call__
return self.app(environ, start_response)
File "/home/odoo/web/7.0/addons/web/http.py", line 487, in __call__
return self.app(environ, start_wrapped)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 591, in __call__
return self.app(environ, start_response)
File "/home/odoo/web/7.0/addons/web/http.py", line 553, in dispatch
result = handler(request)
File "/home/odoo/web/7.0/addons/web/http.py", line 618, in <lambda>
return lambda request: JsonRequest(request).dispatch(method)
File "/home/odoo/web/7.0/addons/web/http.py", line 251, in dispatch
body = simplejson.dumps(response)
File "/usr/local/lib/python2.7/dist-packages/simplejson/__init__.py", line 370, in dumps
return _default_encoder.encode(obj)
File "/usr/local/lib/python2.7/dist-packages/simplejson/encoder.py", line 271, in encode
chunks = list(chunks)
File "/usr/local/lib/python2.7/dist-packages/simplejson/encoder.py", line 632, in _iterencode
for chunk in _iterencode_dict(o, _current_indent_level):
File "/usr/local/lib/python2.7/dist-packages/simplejson/encoder.py", line 591, in _iterencode_dict
for chunk in chunks:
File "/usr/local/lib/python2.7/dist-packages/simplejson/encoder.py", line 591, in _iterencode_dict
for chunk in chunks:
File "/usr/local/lib/python2.7/dist-packages/simplejson/encoder.py", line 642, in _iterencode
o = _default(o)
File "/usr/local/lib/python2.7/dist-packages/simplejson/encoder.py", line 246, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.date(2015, 5, 28) is not JSON serializable

So it looks like it is the operation in _defaults that is incorrect, maybe the two fields aren't compatible with each other too, but I don't know what I should use.

Armand
  • 39
  • 8
  • Can you find a stack trace in the logs? Maybe turn on debug mode if not in production? – amccormack Apr 30 '15 at 18:20
  • I am not sure I understand, are you talking about what the computer terminal tells about the error ? If so, it is : " raise TypeError(repr(o) + " is not JSON serializable") TypeError: datetime.date(2015, 5, 28) is not JSON serializable " There are also reference to a few files that I don't know but the message is quite long, should I paste it all ? In the OpenERP debug mode I can't see the fields I use because the view inherits from several xml files. – Armand Apr 30 '15 at 18:36
  • Yes, that kind of information is helpful. You should include it in your question so it can be formatted correctly. – amccormack Apr 30 '15 at 19:45

1 Answers1

1

Your code will work fine in the latest code. But for your issue you need to return the date as string, not date object in the format expected by the ORM. Make a following change to your code.

from datetime import date
from datetime import timedelta
from dateutil.relativedelta import relativedelta

from openerp.osv import fields, osv
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT


class sale_order_dates(osv.osv):
    _inherit = 'sale.order'

    _columns = {
        'requested_date': fields.date('Requested Date', help="Date requested by the customer for the sale."),
    }

    _defaults = {
        'requested_date': (date.today() + timedelta(days=28)).strftime(DEFAULT_SERVER_DATE_FORMAT),
    }

sale_order_dates()

In version 8, we have a static method in the field definition itself to handle this issue. We only need to do is

fields.Date.to_string(date_obj)
no coder
  • 2,150
  • 14
  • 17
  • It works ! Thank you very much ! If it's not too much to ask, my mission also consisted in setting the requested_date field to Monday if it was initially set on Saturday or Sunday with the operation, so I made conditions in _defaults with the "isoweekday" method of Date, but it seems that _defaults doesn't support conditions, I will add this part to my question with my code. – Armand May 01 '15 at 13:27
  • You need to set dictionary in defaults or You can call a method name directly in defaults and let the method take care of conditions. You can also use lambda directly to calculate the result. You can ask those details in new question. Thanks. – no coder May 01 '15 at 16:48
  • For those who want to know, I asked and answered this second question [here](http://stackoverflow.com/questions/30199586/openerp-7-how-can-i-make-conditions-for-a-fields-default-value) – Armand May 12 '15 at 19:21