Skip to Content
Odoo मेन्यू
  • Sign in
  • मुफ़्त में आज़माएं
  • ऐप्लिकेशन
    फ़ाइनेंस
    • अकाउंटिंग
    • इनवॉइसिंग
    • एक्सपेंस
    • स्प्रेडशीट (बीआई)
    • डॉक्यूमेंट्स
    • साइन
    सेल्स
    • सीआरएम
    • सेल्स
    • पीओएस शॉप
    • पीओएस रेस्टोरेंट
    • सब्सक्रिप्शन
    • रेंटल
    वेबसाइट
    • वेबसाइट बिल्डर
    • ई-कॉमर्स
    • ब्लॉग
    • फ़ोरम
    • लाइव चैट
    • ई-लर्निंग
    सप्लाई चेन
    • इन्वेंट्री
    • मैन्युफ़ैक्चरिंग
    • पीएलएम
    • परचेज़
    • मेंटेनेंस
    • क्वालिटी
    मानव संसाधन
    • कर्मचारी
    • रिक्रूटमेंट
    • टाइम ऑफ़
    • अप्रेज़ल
    • रेफ़रल
    • फ़्लीट
    मार्केटिंग
    • सोशल मार्केटिंग
    • ईमेल मार्केटिंग
    • एसएमएस मार्केटिंग
    • इवेंट
    • मार्केटिंग ऑटोमेशन
    • सर्वे
    सेवाएं
    • प्रोजेक्ट
    • टाइमशीट
    • फ़ील्ड सर्विस
    • हेल्पडेस्क
    • प्लानिंग
    • अपॉइंटमेंट
    प्रॉडक्टिविटी
    • डिस्कस
    • अप्रूवल
    • आईओटी
    • वीओआईपी
    • नॉलेज
    • WhatsApp
    तीसरे पक्ष के ऐप्लिकेशन Odoo स्टूडियो Odoo क्लाउड प्लेटफ़ॉर्म
  • इंडस्ट्री
    रीटेल
    • बुक स्टोर
    • क्लोदिंग स्टोर
    • फ़र्नीचर स्टोर
    • ग्रॉसरी स्टोर
    • हार्डवेयर स्टोर
    • टॉय स्टोर
    Food & Hospitality
    • बार और पब
    • रेस्टोरेंट
    • फ़ास्ट फ़ूड
    • Guest House
    • बेवरिज डिस्ट्रीब्यूटर
    • होटल
    रियल एस्टेट
    • Real Estate Agency
    • आर्किटेक्चर फ़र्म
    • कंसट्रक्शन
    • एस्टेट मैनेजमेंट
    • गार्ड्निंग
    • प्रॉपर्टी ओनर असोसिएशन
    कंसल्टिंग
    • अकाउंटिंग फ़र्म
    • Odoo पार्टनर
    • मार्केटिंग एजेंसी
    • लॉ फ़र्म
    • टैलेंट ऐक्विज़िशन
    • ऑडिट & सर्टिफ़िकेशन
    मैन्युफ़ैक्चरिंग
    • टेक्सटाइल
    • Metal
    • फ़र्नीचर
    • फ़ूड
    • Brewery
    • कॉर्पोरेट गिफ़्ट
    हेल्थ & फिटनेस
    • स्पोर्ट्स क्लब
    • आईवियर स्टोर
    • फिटनेस सेंटर
    • वेलनेस प्रैक्टिशनर
    • फॉर्मेसी
    • हेयर सैलून
    Trades
    • Handyman
    • आईटी हॉर्डवेयर और सपोर्ट
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    अन्य
    • Nonprofit Organization
    • एन्वायरमेंटल एजेंसी
    • बिलबोर्ड रेंटल
    • फ़ोटोग्राफी
    • बाइक लीजिंग
    • सॉफ़्टवेयर रीसेलर
    Browse all Industries
  • कम्यूनिटी
    सीखें
    • ट्यूटोरियल्स
    • दस्तावेज़
    • सर्टिफ़िकेशन
    • ट्रेनिंग
    • ब्लॉग
    • पॉडकास्ट
    शिक्षा को बढ़ावा दें
    • एजुकेशन प्रोग्राम
    • स्केल अप! बिजनेस गेम
    • Odoo के ऑफ़िस में आएं
    सॉफ़्टवेयर पाएं
    • डाउनलोड
    • वर्शन की तुलना करें
    • रिलीज़
    साथ मिलकर काम करें
    • Github
    • फ़ोरम
    • इवेंट
    • अनुवाद
    • पार्टनर बनें
    • Services for Partners
    • अपना अकाउंटिंग फ़र्म रजिस्टर करें
    सेवाएं पाएं
    • पार्टनर ढूंढें
    • अकाउंटेंट खोजें
    • सलाहकार की मदद लें
    • इम्प्लिमेंटेशन सेवाएं
    • कस्टमर रेफ़रेंस
    • सहायता
    • अपग्रेड
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    डेमो देखें
  • कीमत
  • सहायता

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

  • सीआरएम
  • e-Commerce
  • लेखांकन
  • इन्वेंटरी
  • PoS
  • प्रोजेक्ट
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
टैग (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
टैग (View all)
odoo accounting v14 pos v15
About this forum
Help

ValueError: expected singleton

Subscribe

Get notified when there's activity on this post

This question has been flagged
22 Replies
55551 Views
Avatar
Vu Huynh

Hi everybody,

I'm new to odoo, and I created my custom module for contract.

I change the view to update my new field base on the selected resource. My code:


View:
<field name="working_hours" position="replace">
                    <field name="working_hours"
                           on_change="on_change_working_hours(working_hours)"/>
                </field>
Python code:
@api.onchange('working_hours')
    def on_change_working_hours(self, working_hours):
        if working_hours:
            res_calendar = self.env['resource.calendar'].search([('id', '=', working_hours)])
            if res_calendar:
                self.expected_working_hours = res_calendar.expected_working_hours

But when changing the field 'working_hours', I got errror:

    raise ValueError("Expected singleton: %s" % self)
ValueError: Expected singleton: hr.contract()

How can I update my code?

Thank you.



0
Avatar
Discard
Niyas Raphy (Walnut Software Solutions)

Hai Vu,

What is the type of the field expected_working_hours ?

Avatar
Mai Ecarde
Best Answer

View definition with @api.onchange you shouldn't need to call the function and therefore shouldn't need to replace anything.

You could change your python code to accept multiple records.

In python use an iterator "for record in self"

@api.onchange('working_hours') 
def on_change_working_hours(self):
    for record in self:
        if record.working_hours:
            res_calendar = self.env['resource.calendar'].search([('id', '=', record.working_hours)])
            if res_calendar:
                record.expected_working_hours = res_calendar.expected_working_hours
7
Avatar
Discard
Vu Huynh
Author

Hi Ecarde,

I tried but the code in the loop 'for record in self:' is never reached.

Vu Huynh
Author

Hi,

I changed the code and it works

thank you

Mai Ecarde

Glad it solved your problem. Could you mark my post as the answer?

Avatar
Salama Sidna
Best Answer

ValueErrorExpected singleton: financial.grant(1, 3, 4)




0
Avatar
Discard
Avatar
Vu Huynh
Author Best Answer

expected_working_hours is type of Float

0
Avatar
Discard
Rafael Aguero Baquero

Ok, then this code solve your problem, please check the use of decorators, only it's necesary @api.onchange

Warning: Some code was fixed, I'm assuming working_hours it's m2o field to resource.calendar entity.

@api.onchange('working_hours')

def on_change_working_hours(self):

self.expected_working_hours = self.working_hours.expected_working_hours if self.working_hours else 0.

Please, confirm me if this answer solve your question.

Vu Huynh
Author

Hi Rafael,

This code doesn't fix my issue.

But, I updated my code like this, and it works:

@api.onchange('working_hours')

def onchange_working_hours(self, working_hours):

result = {}

if working_hours:

res_calendar = self.env['resource.calendar'].search([('id', '=', working_hours)])

if res_calendar:

result['expected_working_hours'] = res_calendar.expected_working_hours

return {'value': result}

Avatar
Nikhil Krishnan
Best Answer

Hi Huynh,
I think your model is hr.contract,
 and  Expected singleton: hr.contract  is error because of self contain multiple records,

Am not guaranteed, if you change in the code in python it will solve the singleton error,

@api.onchange('working_hours')
def on_change_working_hours(self, working_hours):
if working_hours:
res_calendar = self.env['resource.calendar'].search([('id', '=', working_hours.id)])
if res_calendar:
for s in self:
s.expected_working_hours = res_calendar.expected_working_hours
     Also one think that always make code more clear and give the complete one with Model and all.

0
Avatar
Discard
Vu Huynh
Author

Hi Krishnan,

This change does not solve my problem, the code:

s.expected_working_hours =...

is never reached.

This is fully my code:

class Contract(models.Model):

_inherit = 'hr.contract'

expected_working_hours = fields.Float('Expected Working Hours',

required=True)

@api.onchange('working_hours')

def onchange_working_hours(self, working_hours):

if working_hours:

res_calendar = self.env['resource.calendar'].search([('id', '=', working_hours)])

if res_calendar:

self.expected_working_hours = res_calendar.expected_working_hours

Niyas Raphy (Walnut Software Solutions)

Hi Vu,

can you print and check what you are getting in the res_calendar & res_calendar.expected_working_hours ?

res_calendar = self.env['resource.calendar'].search([('id', '=', working_hours)])

if res_calendar:

print res_calender

print res_calendar.expected_working_hours

self.expected_working_hours = res_calendar.expected_working_hours

Vu Huynh
Author

I think the problem is from the line

self.expected_working_hours = res_calendar.expected_working_hours

I debug and got correct value

but the problem still happened with a constant number

Nikhil Krishnan

res_calendar = self.env['resource.calendar'].search([('id', '=', working_hours.id)])

working_hours is a record, use the id of that record.

Vu Huynh
Author

Hi Krishnan,

I think it isn't the issue I got, I can find the correct resource.calendar with working_hours

But, the problem is when assign the value to self.expected_working_hours.

It also happened when I just try:

self.expected_working_hours = 40

Avatar
Rafael Aguero Baquero
Best Answer

At first, if you use the new api as it seems do not need to declare the onchange in the view, with the decorator in python is enough. Next, i assume that working_hours is a Many2one id because otherwise there may be the problem.

Then:

There isn't need to declare view inheritance

Python code:
@api.onchange('working_hours') def on_change_working_hours(self):
    if working_hours:
         self.expected_working_hours = self.env['resource.calendar'].search([('id', '=', working_hours)]).id or False
0
Avatar
Discard
Vu Huynh
Author

The problem is when assign the value to self.expected_working_hours.

It also happened when I just try:

self.expected_working_hours = 40

Rafael Aguero Baquero

Maybe yo need try tos ways:

1. Declare expected_workings_hours as a computed field if it'll always be a formula.

2. Try with a write: self.write{'expected_workings_hours':40} but I don't recommend this.

Vu Huynh
Author

Hi Rafael,

The field 'expected_working_hours' is not always a formula field. It's computed, but user can change it.

Rafael Aguero Baquero

Try force search code line to limit 1 like this:

self.env['resource.calendar'].search([('id', '=', working_hours)], limit=1)

Otherwise i will need to see expected_working_hours field declaration because i can't find it in odoo source code

Vu Huynh
Author

No, the 'expected_working_hours' is my new field.

I declare it both in 'resource_calendar' and 'hr_contract', type of float.

By default, the hr_contract.expected_working_hours will be get from resource_calendar. But, user can change it.

Rafael Aguero Baquero

Ok, then this code solve your problem, please check the use of decorators, only it's necesary @api.onchange

Warning: Some code was fixed, I'm assuming working_hours it's m2o field to resource.calendar entity.

@api.onchange('working_hours')

def on_change_working_hours(self):

self.expected_working_hours = self.working_hours.expected_working_hours if self.working_hours else 0.

Please, confirm me if this answer solve your question.

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
कम्यूनिटी
  • ट्यूटोरियल्स
  • दस्तावेज़
  • फ़ोरम
ओपन सोर्स
  • डाउनलोड
  • Github
  • रनबॉट
  • अनुवाद
सेवाएं
  • Odoo.sh Hosting
  • सहायता
  • अपग्रेड
  • कस्टम डेवलपमेंट्स
  • शिक्षा
  • अकाउंटेंट खोजें
  • पार्टनर ढूंढें
  • पार्टनर बनें
हमारे बारे में
  • हमारी कंपनी
  • ब्रांड ऐसेट
  • संपर्क करें
  • नौकरियां
  • इवेंट
  • पॉडकास्ट
  • ब्लॉग
  • ग्राहक
  • लीगल • गोपनीयता
  • सुरक्षा
الْعَرَبيّة 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, बिज़नेस से जुड़े ऐप्लिकेशन का एक कलेक्शन है जो ओपन सोर्स पर आधारित है. इसमें आपकी कंपनी की हर ज़रूरत के लिए ऐप्लिकेशन हैं. जैसे, सीआरएम, ई-कॉमर्स, अकाउंटिंग, इन्वेंट्री, पॉइंट ऑफ़ सेल, प्रोजेक्ट मैनेजमेंट वगैरह.

Odoo की सबसे बड़ी खासियत है कि यह इस्तेमाल करने में बहुत आसान है और यह पूरी तरह से इंटिग्रेट किया हुआ है.

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