关于python:openerp7:如何在创建表单中设置默认日期?

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

我不熟悉openerp和python,我试图在创建表单中设置一个默认日期,该日期必须是用户使用创建表单后28天。

我试过的最后一件事是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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()

但是如果我打开创建表单,就会得到这个错误:

"服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。"

这可能是因为我不能在默认情况下进行操作,但是我不知道该怎么做,我试图在函数中进行操作,但是我对函数还不是很满意。你知道我该怎么做吗?提前谢谢

编辑:这是计算机终端上的错误消息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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

所以看起来是默认值中的操作不正确,可能这两个字段也不兼容,但我不知道应该使用什么。


在最新的代码中,您的代码可以正常工作。但对于您的问题,您需要以字符串形式返回日期,而不是以ORM期望的格式返回日期对象。对代码进行以下更改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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()

在版本8中,我们在字段定义本身中有一个静态方法来处理这个问题。我们只需要做的是

1
fields.Date.to_string(date_obj)