Skip to Content
Odoo Menu
  • Prijavi
  • Try it free
  • Aplikacije
    Finance
    • Knjigovodstvo
    • Obračun
    • Stroški
    • Spreadsheet (BI)
    • Dokumenti
    • Podpisovanje
    Prodaja
    • CRM
    • Prodaja
    • POS Shop
    • POS Restaurant
    • Naročnine
    • Najem
    Spletne strani
    • Website Builder
    • Spletna trgovina
    • Blog
    • Forum
    • Pogovor v živo
    • eUčenje
    Dobavna veriga
    • Zaloga
    • Proizvodnja
    • PLM
    • Nabava
    • Vzdrževanje
    • Kakovost
    Kadri
    • Kadri
    • Kadrovanje
    • Odsotnost
    • Ocenjevanja
    • Priporočila
    • Vozni park
    Marketing
    • Družbeno Trženje
    • Email Marketing
    • SMS Marketing
    • Dogodki
    • Avtomatizacija trženja
    • Ankete
    Storitve
    • Projekt
    • Časovnice
    • Storitve na terenu
    • Služba za pomoč
    • Načrtovanje
    • Termini
    Produktivnost
    • Razprave
    • Odobritve
    • IoT
    • Voip
    • Znanje
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industrije
    Trgovina na drobno
    • Book Store
    • Trgovina z oblačili
    • Trgovina s pohištvom
    • Grocery Store
    • Trgovina s strojno opremo računalnikov
    • Trgovina z igračami
    Food & Hospitality
    • Bar and Pub
    • Restavracija
    • Hitra hrana
    • Guest House
    • Beverage Distributor
    • Hotel
    Nepremičnine
    • Real Estate Agency
    • Arhitekturno podjetje
    • Gradbeništvo
    • Estate Management
    • Vrtnarjenje
    • Združenje lastnikov nepremičnin
    Svetovanje
    • Računovodsko podjetje
    • Odoo Partner
    • Marketinška agencija
    • Law firm
    • Pridobivanje talentov
    • Audit & Certification
    Proizvodnja
    • Tekstil
    • Metal
    • Pohištvo
    • Hrana
    • Brewery
    • Poslovna darila
    Health & Fitness
    • Športni klub
    • Trgovina z očali
    • Fitnes center
    • Wellness Practitioners
    • Lekarna
    • Frizerski salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Sistemi sončne energije
    • Izdelovalec čevljev
    • Čistilne storitve
    • HVAC Services
    Ostali
    • Neprofitna organizacija
    • Agencija za okolje
    • Najem oglasnih panojev
    • Fotografija
    • Najem koles
    • Prodajalec programske opreme
    Browse all Industries
  • Skupnost
    Learn
    • Tutorials
    • Dokumentacija
    • Certifikati
    • Šolanje
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Prenesi
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Dogodki
    • Prevodi
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Sklici kupca
    • Podpora
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Določanje cen
  • Pomoč

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

  • CRM
  • e-Commerce
  • Knjigovodstvo
  • Zaloga
  • PoS
  • Projekt
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
Pomoč

ValueError: expected singleton

Naroči se

Get notified when there's activity on this post

This question has been flagged
22 Odgovori
55547 Prikazi
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
Opusti
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
Opusti
Vu Huynh
Avtor

Hi Ecarde,

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

Vu Huynh
Avtor

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
Opusti
Avatar
Vu Huynh
Avtor Best Answer

expected_working_hours is type of Float

0
Avatar
Opusti
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
Avtor

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
Opusti
Vu Huynh
Avtor

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
Avtor

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
Avtor

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
Opusti
Vu Huynh
Avtor

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
Avtor

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
Avtor

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!

Prijavi
Community
  • Tutorials
  • Dokumentacija
  • Forum
Open Source
  • Prenesi
  • Github
  • Runbot
  • Prevodi
Services
  • Odoo.sh Hosting
  • Podpora
  • Nadgradnja
  • Custom Developments
  • Izobraževanje
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Sredstva blagovne znamke
  • Kontakt
  • Zaposlitve
  • Dogodki
  • Podcast
  • Blog
  • Stranke
  • Pravno • Zasebnost
  • Varnost
الْعَرَبيّة 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