Skip to Content
Odoo Menu
  • Sign in
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

Doubt in onchange function, wants to return date.

Subscribe

Get notified when there's activity on this post

This question has been flagged
2 Replies
3566 Views
Avatar
rosey

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
Avatar
Discard
Avatar
Narayanamurthy
Best Answer

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 }

 

 

0
Avatar
Discard
Dimple

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

Ivan

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

Avatar
Temur
Best Answer

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,

0
Avatar
Discard
Dimple

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

Temur
-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

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... < >

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now