Hilfe

1

How to Date deduction with Python ?

Avatar
priyankahdp

here shows date of birth field in my openerp model

'date_of_birth': fields.date('Date of Birth'),

need to change its default date to 25years earlier.because its easier to user to pick year. ( in openerp jquery default load current 20years in list and user have to get some time to select earlier year ).

for ex :

_defaults = {
'date_of_birth':fields.date.context_today - 25years

please advice me to implement this issue (if its with python function seems good for my requirement)

---------------EDITED---------------

@ Dear Patently,

I tried it with my console.its gives a error

>>> import datetime
>>> from datetime import timedelta
>>> diff = datetime.datetime.now() - datetime.timedelta(years=42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'years' is an invalid keyword argument for this function

I tried it with openerp in eclipse

def _dob(self, cr, uid, context=None):
    diff = datetime.datetime.now() - datetime.timedelta(years=42)
    return diff

also gives error

  File "/home/bellvantage/Documents/openerp-7.0/openerp-7/openerp/addons/bpl/bpl.py", line 47, in _dob
    diff = datetime.datetime.now() - datetime.timedelta(years=42)
TypeError: 'years' is an invalid keyword argument for this function
Avatar
Verwerfen
2 Antworten
4
Best Answer

try this:

import datetime
from datetime import timedelta
diff = datetime.datetime.now() - datetime.timedelta(years=42)
1 Kommentar
Avatar
Verwerfen
Avatar
priyankahdp
-

ur answer very helped me to my code

0
Avatar
priyankahdp
Best Answer

now its works with this code

def _dob(self, cr, uid, context=None):
    today = datetime.date.today()
    past = today + relativedelta.relativedelta(years= -25)
    return past.strftime('%Y-%m-%d')

thanks all answers

Avatar
Verwerfen