OpenERP 7:如何为字段的默认值设置条件? [英] OpenERP 7 : How can I make conditions for a field's default value?

查看:113
本文介绍了OpenERP 7:如何为字段的默认值设置条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我关于

This is the second step of my question about setting a default date in create form, I move it here to clarify the two questions :

我的任务还包括:如果最初通过操作将周六或周日设置为required_date字段,则将其设置为周一,因此我使用Date的"isoweekday"方法在_defaults中设置了条件,但是_defaults似乎没有支持条件:

My mission also consisted in setting the requested_date field to Monday if it was initially set on Saturday or Sunday with the operation, so I made conditions in _defaults with the "isoweekday" method of Date, but it seems that _defaults doesn't support conditions :

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

The computer terminal tells me that the syntax is invalid where I use "if", it is the first time I use conditions so maybe I just didn't write them like I have to.

这是我收到的完整消息:

This is the complete message I get :

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

推荐答案

对于那些想知道的人,对于我的问题的第二部分(避免将request_date放在周末),我已经编写了此函数,看来正常工作(至少它仍然显示创建后28天的日期),我没有办法对其进行测试,但是至少当我以此来更新数据库或尝试创建数据库时,我没有错误.此字段所在的表单:

For those who want to know, for the second part of my question (avoiding requested_date to be in week-end), I have written this function and it seems working (at least it still shows the date of 28 days after the creation), I don't have the means to test it but at least I don't have errors when I update the database with this or when I try to create the form where this field is :

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,没有编码员的帮助!

Thanks to amccormack and no coder for their help !

这篇关于OpenERP 7:如何为字段的默认值设置条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆