Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How can I set the default value of a field to 'today'?

By
Dr Obx
on 1/6/16, 9:16 AM 399 views

Guys, I would like to have in my module start date and end date set automatically and I have but both st_date and en_date is set "today":

I need:

end date = today

start date = 30 days before today (approximately a month before end date)

code i'm using :

 def _days_between(self, cr, uid, ids, field_name, arg, context=None):

res = {}

for cur_data in self.browse(cr, uid, ids, context=context):

st_date = datetime.strptime(cur_data.st_date, "%Y-%m-%d")

en_date = datetime.strptime(cur_data.en_date, "%Y-%m-%d")

res[cur_data.id] = abs((en_date - st_date).days)

return res

def _weeks_between(self, cr, uid, ids, field_name, arg, context=None):

res = {}

for cur_data in self.browse(cr, uid, ids, context=context):

st_date = datetime.strptime(cur_data.st_date, "%Y-%m-%d")

en_date = datetime.strptime(cur_data.en_date, "%Y-%m-%d")

week_day1 = (st_date - timedelta(days=st_date.weekday()))

week_day2 = (en_date - timedelta(days=en_date.weekday()))

res[cur_data.id] = (week_day2 - week_day1).days / 7

return res

def _months_between(self, cr, uid, ids, field_name, arg, context=None):

res = {}

for cur_data in self.browse(cr, uid, ids, context=context):

st_date = datetime.strptime(cur_data.st_date, "%Y-%m-%d")

en_date = datetime.strptime(cur_data.en_date, "%Y-%m-%d")

res[cur_data.id] = abs((en_date - st_date).days) / 30

return res


_columns = {

'product': fields.one2many('product.data','check_id', string="Product"),

'location_id': fields.many2one('stock.location','location'),

'st_date': fields.date('Start date'),

'en_date': fields.date('End date'),

'wr_date': fields.char('Date'),

'nod': fields.function(_days_between, type='integer', string='NoD'),

'now': fields.function(_weeks_between, type='integer', string='NoW'),

'nom': fields.function(_months_between, type='integer', string='NoM'),

}

_defaults = {

'st_date': lambda *a: time.strftime('%Y-%m-%d'),#30 days before end date

'en_date': lambda *a: time.strftime('%Y-%m-%d'),

'location_id': _get_def_loc,

}

How to set defaults?

2

Tarek Mohamed Ibrahim

--Tarek Mohamed Ibrahim--
896
| 5 3 7
Gîza, Egypt
--Tarek Mohamed Ibrahim--

I am an old VFP developer on ERP

I have moved to 2p since Nov-2014 and started developing with Python on Odoo.

https://www.linkedin.com/profile/public-profile-settings?trk=prof-edit-edit-public_profile

tibrahim@2p.com.sa

Tarek Mohamed Ibrahim
On 1/6/16, 12:37 PM

use timedelta as follows

from datetime import datetime, timedelta  
....
_defaults = {
.....
'st_date':lambda *a: datetime.strftime(datetime.now()-timedelta(days=30) ,'%Y-%m-%d'),
....
}

this will give you the result you requested

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 1/6/16, 9:16 AM
Seen: 399 times
Last updated: 1/7/16, 1:12 PM