简体   繁体   English

OpenERP 7:如何在创建表单中设置默认日期?

[英]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. 我是OpenERP和Python的新手,我试图在创建表单中设置默认日期,该日期必须是用户使用创建表单后的28天。

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. 可能是因为我无法使用_defaults进行操作,但是后来我不知道该怎么做,我试图在函数中进行操作,但是我对函数还不太满意。 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. 因此,看来_defaults中的操作不正确,也许这两个字段也互不兼容,但我不知道该使用什么。

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. 但是对于您的问题,您需要以字符串形式返回日期,而不是以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. 版本8中 ,我们在字段定义本身中提供了一个静态方法来解决此问题。 We only need to do is 我们唯一要做的是

fields.Date.to_string(date_obj)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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