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

Doubt in onchange function, wants to return date.

By
rosey
on 9/12/14, 8:20 AM 861 views

I have four fields

join - date field

rejoin - date field

date - date field

days - char

<field name="join" on_change="on_change_join(date_join)"/>

<field name="rejoin" on_change="on_change_rejoin(date_rejoin)"/>

<field name="date" />

<field name="days" on_change="on_change_rejoin(days)"/>

 

When we enter join/rejoin date field, it will call the onchange function.

def on_change_join/rejoin(self, cr, uid, ids, join/rejoin, context=None):
          records = self.browse(cr, uid, ids, context=context)
          res = {}
          for r in records:
            
              vacation_due = 0
              format_string =  "%Y-%m-%d"
              if r.join/rejoin:
                  datetime_object = datetime.strptime(r.join/rejoin, format_string).date()
                  print "datetime_object",datetime_object
                  new_date = datetime_object + relativedelta(years=2)
                  print "new_date",new_date
                  new_date_string = datetime.strftime(new_date,format_string)
                  print "new_date_string",new_date_string
              
              
              return {'value': {'date': new_date_string}}

But it doesnt shows clear output. ie, it shows error when i first enter join/rejoin date

Error: return {'value': {'date': new_date_string}}

UnboundLocalError: local variable 'new_date_string' referenced before assignment

 

 

When i enter days, it will sum up with date field. But it also shows error

 

def my_onchange_days(self, cr, uid, ids,days, context=None):

 return {'value': {'date': (datetime.strptime(date,'%Y-%m-%d %H:%M:%S') + relativedelta(days=days)).strftime('%Y-%m-%d %H:%M:%S')}}

 Please help me any one to solve this.

 

 

 

0
Narayanamurthy
On 9/12/14, 8:35 AM

Hi rosey, i either of both tho functions you need to check the field value befor doing and calculation on it

def on_change_join/rejoin(self, cr, uid, ids, join/rejoin, context=None):
          records = self.browse(cr, uid, ids, context=context)
          res = {}

          new_date_string = False
          for r in records:
              vacation_due = 0
              format_string =  "%Y-%m-%d"
              if r.join/rejoin:
                  datetime_object = datetime.strptime(r.join/rejoin, format_string).date()
                  print "datetime_object",datetime_object
                  new_date = datetime_object + relativedelta(years=2)
                  print "new_date",new_date
                  new_date_string = datetime.strftime(new_date,format_string)
                  print "new_date_string",new_date_string

       return {'value': {'date': new_date_string}}

And

def my_onchange_days(self, cr, uid, ids,days, context=None):
    res = {}
    if days:
        res['date'] : (datetime.strptime(date,'%Y-%m-%d %H:%M:%S') + relativedelta(days=days)).strftime('%Y-%m-%d %H:%M:%S')}
return {'value': res }

 

 

hai, i getting this error while doing this code res['date'] : (datetime.strptime(date,'%Y-%m-%d %H:%M:%S') + ^ relativedelta(days=days)).strftime('%Y-%m-%d %H:%M:%S')} SyntaxError: invalid syntax

Dimple
on 9/13/14, 6:19 AM

Did you notice the ^ sign in front of relativedelta that you have pasted?

Ivan
on 9/13/14, 2:33 PM
0

Temur

--Temur--
2896
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 9/12/14, 8:38 AM

hi,

for resolve "UnboundLocalError: local variable 'new_date_string' referenced before assignment", try:

def on_change_join/rejoin(self, cr, uid, ids, join/rejoin, context=None):
          records = self.browse(cr, uid, ids, context=context)
          res = {}
          vals = {}
          for r in records:
            
              vacation_due = 0
              format_string =  "%Y-%m-%d"
              if r.join/rejoin:
                  datetime_object = datetime.strptime(r.join/rejoin, format_string).date()
                  print "datetime_object",datetime_object
                  new_date = datetime_object + relativedelta(years=2)
                  print "new_date",new_date
                  new_date_string = datetime.strftime(new_date,format_string)
                  vals['date'] = new_date_string
                  print "new_date_string",new_date_string
              
              
              return {'value': vals}

 

reagards,

hai, i tried this code but it didn't returning any value. what will i do to return the value?

Dimple
on 9/13/14, 1:07 AM
-in this line, why you're passing 'date_rejoin' as parameter in XML? you've another field named "date_rejoin"? if you're trying to pass this same field, then please try instead. I mean that you've to use parameter name exactly as it appears in 'name' paroperty (name="rejoin"...). Same for other fields.
Temur
on 9/13/14, 2:40 AM

in this line: field name="rejoin" on_change="on_change_rejoin(date_rejoin) try instead: field name="rejoin" on_change="on_change_rejoin(rejoin) -these parts disappeared from my previous comment, due to xml tags... < >

Temur
on 9/13/14, 2:48 AM

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

2 follower(s)

Stats

Asked: 9/12/14, 8:20 AM
Seen: 861 times
Last updated: 3/16/15, 8:10 AM