简体   繁体   English

OpenERP 7:如何为字段的默认值设置条件?

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

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 : 这是我关于在create form中设置默认日期的问题的第二步,我将其移至此处以阐明两个问题:

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 : 我的任务还包括:如果最初通过操作将该请求的日期设置为星期六或星期日,则将request_date字段设置为星期一,因此我使用Date的“ isoweekday”方法在_defaults中设置了条件,但_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),
}

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. 计算机终端告诉我,语法在我使用“ if”的地方无效,这是我第一次使用条件,所以也许我只是没有像必须那样写。

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

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 : 对于那些想知道的人,对于我的问题的第二部分(避免将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,
}

Thanks to amccormack and no coder for their help ! 感谢amccormack,没有编码器的帮助!

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

相关问题 OpenERP 7:如何在创建表单中设置默认日期? - OpenERP 7 : How can I set a default date in a create form? openerp:如何在日期字段上添加功能以发送通知 - openerp: how i can add function on date field to send notification 我可以在字段构造函数之外设置 StringField 的默认值吗? - Can I set a StringField's default value outside the field constructor? OpenERP 7:如何使按钮在剪贴板中插入预定义的文本? - OpenERP 7 : How can I make a button inserting a predefined text in the Clipboard? 如何在openerp的关系字段中设置值 - how to set value in relational field in openerp 如何根据其他字段值更改OpenERP选择字段中的选项? - How can I change the choices in an OpenERP selection field based on other field values? 如果用户输入不是目录,如何将目录设为默认值 - if user input is not a directory, how can I make the directory a default value 如何使用 groupby 在给定一些预定义条件的情况下确定字段的最后一个值? - How can I determine the last value of a field given some predefined conditions using groupby? 如何使我的字段值不从标签浮动? - how can i make my field value not floating from lable? 如何根据一组条件在 PANDAS 中创建一个新列,然后将新列设置为另一个字段的值 - How can I create a new column in PANDAS based on a set of conditions and then setting the new column to the value of another field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM