这是我关于setting a default date in create form的问题的第二步,我在这里提出澄清两个问题:
我的任务还包括将requested_date字段设置为星期一,如果它最初是在星期六或星期日设置的,那么我在_defaults中使用“isoweekday”方法设置了条件,但似乎_defaults没有支持条件:
_defaults = {
'requested_date': (date.today() + timedelta(days=28)).strftime(DEFAULT_SERVER_DATE_FORMAT),
if requested_date.isoweekday = 6
'requested_date': (date.today() + timedelta(days=30)).strftime(DEFAULT_SERVER_DATE_FORMAT),
if requested_date.isoweekday = 7
'requested_date': (date.today() + timedelta(days=29)).strftime(DEFAULT_SERVER_DATE_FORMAT),
}
计算机终端告诉我语法无效,我使用“if”,这是我第一次使用条件,所以也许我只是没有像我一样写它们。
这是我得到的完整信息:
2015-05-01 13:30:35,234 19255 CRITICAL Armand openerp.modules.module: Couldn't load module sale_order_dates
2015-05-01 13:30:35,235 19255 CRITICAL Armand openerp.modules.module: invalid syntax (sale_order_dates.py, line 69)
2015-05-01 13:30:35,235 19255 ERROR Armand openerp: Failed to initialize database `Armand`.
Traceback (most recent call last):
File "/home/odoo/server/7.0/openerp/cli/server.py", line 97, in preload_registry
db, registry = openerp.pooler.get_db_and_pool(dbname,update_module=update_module)
File "/home/odoo/server/7.0/openerp/pooler.py", line 33, in get_db_and_pool registry = RegistryManager.get(db_name, force_demo, status, update_module)
File "/home/odoo/server/7.0/openerp/modules/registry.py", line 203, in get update_module)
File "/home/odoo/server/7.0/openerp/modules/registry.py", line 233, in new openerp.modules.load_modules(registry.db, force_demo, status, update_module)
File "/home/odoo/server/7.0/openerp/modules/loading.py", line 350, in load_modules
force, status, report, loaded_modules, update_module)
File "/home/odoo/server/7.0/openerp/modules/loading.py", line 256, in load_marked_modules
loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks)
File "/home/odoo/server/7.0/openerp/modules/loading.py", line 159, in load_module_graph
load_openerp_module(package.name)
File "/home/odoo/server/7.0/openerp/modules/module.py", line 405, in load_openerp_module
__import__('openerp.addons.' + module_name)
File "/home/odoo/server/7.0/openerp/modules/module.py", line 133, in load_module
mod = imp.load_module('openerp.addons.' + module_part, f, path, descr)
File "/home/odoo/addons/7.0/sale_order_dates/__init__.py", line 22, in <module>
import sale_order_dates
File "/home/odoo/addons/7.0/sale_order_dates/sale_order_dates.py", line 69
if requested_date.isoweekday = 6:
^
SyntaxError: invalid syntax
答案 0 :(得分:2)
对于那些想知道的人,对于我的问题的第二部分(避免requested_date在周末),我已经写了这个函数,它似乎有效(至少它仍然显示了28天后的日期)创建),我没有办法测试它,但至少我没有错误,当我用这个更新数据库或当我尝试创建此字段所在的表单时:
def _set_requested_date(self, cr, uid, ids, context=None):
requested_date = (date.today() + timedelta(days=28)).strftime(DEFAULT_SERVER_DATE_FORMAT),
day = date.today()
if day.isoweekday() == 6:
requested_date = (date.today() + timedelta(days=30)).strftime(DEFAULT_SERVER_DATE_FORMAT),
if day.isoweekday() == 7:
requested_date = (date.today() + timedelta(days=29)).strftime(DEFAULT_SERVER_DATE_FORMAT),
return requested_date
_defaults = {
'requested_date': _set_requested_date,
}
感谢amccormack,没有编码人员的帮助!