问题
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 :
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),
}
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
回答1:
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,
}
Thanks to amccormack and no coder for their help !
来源:https://stackoverflow.com/questions/30199586/openerp-7-how-can-i-make-conditions-for-a-fields-default-value