简体   繁体   中英

OpenERP 7 : How can I set a default date in a create form?

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM