Reputation: 285
I'm working at creating a module for OpenERP 7 to set Today's Date as default value when creating a new Partner. I've installed the module, restarted the Openerp service and defaults aren't changed a bit. (I'd included a "goofball" field and bogus default data for the website field to make sure it's not a python lambda code problem. It wasn't...) Here's my code in partner.py:
from osv import osv, fields
import datetime
class res_partner(osv.osv):
_inherit = 'res.partner'
_columns = {"goofball":fields.char('goofball', size=15)}
_defaults = {
'website': 'www.veppsight.com',
'date': lambda *a: datetime.date.today().strftime('%Y-%m-%d'),
}
The default data isn't entered for the website & date fields and the "goofball" field isn't create in the database which I verified in psql. What am I doing wrong?
Upvotes: 4
Views: 7497
Reputation: 101
_defaults = {
'date': lambda self,cr,uid,context={}: context.get('date', fields.date.context_today(self,cr,uid,context=context)),
or
'date': lambda self, cr, uid, context={}: context.get('date', time.strftime("%Y-%m-%d %H:%M:%S")),
or
'date' : fields.date.context_today,
}
Lambda is a in line or anonymous function in python.
Upvotes: 1
Reputation: 116
Since V6.1 there is a new function to deal with today's date, called context_today.
You can check background on this at the following link... http://openerp-expert-framework.71550.n3.nabble.com/Bug-925361-Re-6-1-date-values-that-are-initialized-as-defaults-may-appear-as-quot-off-by-one-day-quoe-td3741270.html
Based on that, you can just use...
_ defaults = {
'date1': fields.date.context_today,
}
Regards, -Mario
Upvotes: 10
Reputation: 3966
How about this:
from osv import osv, fields import time class res_partner(osv.osv): _inherit = 'res.partner' _columns = {"goofball": fields.char('goofball', size=15)} _defaults = { 'website': 'www.veppsight.com', 'date1': lambda *a: time.strftime('%Y-%m-%d'), } res_partner()
Upvotes: 0
Reputation: 5044
Import time and In the defaults
_defaults = {
'website': 'www.veppsight.com',
'date': lambda *a: time.strftime('%Y-%m-%d'),
}
Upvotes: 2
Reputation: 6295
Use following code :
from osv import osv, fields
import time
class res_partner(osv.osv):
_inherit = 'res.partner'
_columns = {"goofball":fields.char('goofball', size=15)}
_defaults = {
'website': 'www.veppsight.com',
'date1': time.strftime('%Y-%m-%d'),
}
AND IF POSSIBLE RENAME FIELD NAME DATE
TO SOMETHING ELSE. AS DATE IS DT IN POSTGRESQL
Thank You
Upvotes: 1