关于python:OpenERP 7:如何为字段的默认值创建条件?

OpenERP 7 : How can I make conditions for a field's default value?

这是我关于在创建表单中设置默认日期的问题的第二步,我将其移到此处以澄清这两个问题:

我的任务还包括将请求的日期字段设置为星期一(如果它最初是在星期六或星期日进行操作),因此我使用日期的"isoweekday"方法设置了默认值中的条件,但似乎默认值不支持条件:

1
2
3
4
5
6
7
_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"的地方,语法是无效的,这是我第一次使用条件,所以也许我没有像必须这样写它们。

这是我得到的完整信息:

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
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


对于那些想知道的人,在我问题的第二部分(避免被请求的日期在周末),我已经编写了这个函数,它似乎可以工作(至少它仍然显示了创建后28天的日期),我没有办法测试它,但至少当我用这个更新数据库或尝试创建数据库时,我没有错误。此字段所在的窗体:

1
2
3
4
5
6
7
8
9
10
11
12
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和没有编码员的帮助!