Přejít na obsah
Odoo Menu
  • Přihlásit se
  • Vyzkoušejte zdarma
  • Aplikace
    Finance
    • Účetnictví
    • Fakturace
    • Výdaje
    • Spreadsheet (BI)
    • Dokumenty
    • Podpisy
    Prodej
    • CRM
    • Prodej
    • POS Obchod
    • POS Restaurace
    • Předplatné
    • Pronájem
    Webové stránky
    • Webové stránky
    • E-shop
    • Blog
    • Fórum
    • Živý chat
    • eLearning
    Dodavatelský řetězec
    • Sklad
    • Výroba
    • PLM
    • Nákup
    • Údržba
    • Kvalita
    Lidské zdroje
    • Zaměstnanci
    • Nábor
    • Volno
    • Hodnocení zaměstnanců
    • Doporučení
    • Vozový park
    Marketing
    • Marketing sociálních sítí
    • Emailový marketing
    • SMS Marketing
    • Události
    • Marketingová automatizace
    • Dotazníky
    Služby
    • Projekt
    • Časové výkazy
    • Práce v terénu
    • Helpdesk
    • Plánování
    • Schůzky
    Produktivita
    • Diskuze
    • Schvalování
    • IoT
    • VoIP
    • Znalosti
    • WhatsApp
    Aplikace třetích stran Odoo Studio Odoo cloudová platforma
  • Branže
    Maloobchod
    • Knihkupectví
    • Obchod s oblečením
    • Obchod s nábytkem
    • Potraviny
    • Obchod s hardwarem
    • Hračkářství
    Jídlo a pohostinství
    • Bar a Pub
    • Restaurace
    • Fast Food
    • Penzion
    • Distributor nápojů
    • Hotel
    Nemovitost
    • Realitní kancelář
    • Architektonická firma
    • Stavba
    • Správa nemovitostí
    • Zahradnictví
    • Asociace vlastníků nemovitosti
    Poradenství
    • Účetní firma
    • Odoo Partner
    • Marketingová agentura
    • Právník
    • Akvizice talentů
    • Audit a certifikace
    Výroba
    • Textil
    • Kov
    • Nábytek
    • Jídlo
    • Pivovar
    • Korporátní dárky
    Zdraví a fitness
    • Sportovní klub
    • Prodejna brýli
    • Fitness Centrum
    • Wellness praktikové
    • Lékárna
    • Kadeřnictví
    Transakce
    • Údržbář
    • Podpora IT & hardware
    • Systémy solární energie
    • Výrobce obuvi
    • Úklidové služby
    • Služby HVAC
    Ostatní
    • Nezisková organizace
    • Agentura pro životní prostředí
    • Pronájem billboardů
    • Fotografování
    • Leasing jízdních kol
    • Prodejce softwaru
    Procházet všechna odvětví
  • Komunita
    Edukační program
    • Tutoriály
    • Dokumentace
    • Certifikace
    • Vzdělávání
    • Blog
    • Podcast
    Podpora vzdělávání
    • Vzdělávací program
    • Scale Up! Hra na firmu
    • Navštivte Odoo
    Získat software
    • Stáhnout
    • Porovnejte edice
    • Verze
    Spolupráce
    • Github
    • Fórum
    • Události
    • Překlady
    • Stát se partnerem
    • Služby pro partnery
    • Registrujte svou účetní firmu
    Získat služby
    • Najít partnera
    • Najít účetní
    • Setkejte se s poradcem
    • Implementační služby
    • Zákaznické reference
    • Podpora
    • Upgrady
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Dohodnout demo
  • Ceník
  • Pomoc

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

  • CRM
  • e-Commerce
  • Účetnictví
  • Sklad
  • PoS
  • Projekty
  • MRP
All apps
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
You need to be registered to interact with the community.
All Posts Lidé Odznaky
Štítky (View all)
odoo accounting v14 pos v15
O tomto fóru
Pomoc

ValueError: expected singleton

Odebírat

Get notified when there's activity on this post

This question has been flagged
22 Odpovědi
55549 Zobrazení
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
Zrušit
Niyas Raphy (Walnut Software Solutions)

Hai Vu,

What is the type of the field expected_working_hours ?

Avatar
Mai Ecarde
Nejlepší odpověď

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
Zrušit
Vu Huynh
Autor

Hi Ecarde,

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

Vu Huynh
Autor

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
Nejlepší odpověď

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




0
Avatar
Zrušit
Avatar
Vu Huynh
Autor Nejlepší odpověď

expected_working_hours is type of Float

0
Avatar
Zruš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.

Vu Huynh
Autor

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
Nejlepší odpověď

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
Zrušit
Vu Huynh
Autor

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
Autor

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
Autor

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
Nejlepší odpověď

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
Zrušit
Vu Huynh
Autor

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
Autor

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
Autor

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!

Přihlásit se
Komunita
  • Tutoriály
  • Dokumentace
  • Fórum
Open Source
  • Stáhnout
  • Github
  • Runbot
  • Překlady
Služby
  • Odoo.sh hostování
  • Podpora
  • Upgrade
  • Nestandardní vývoj
  • Edukační program
  • Najít účetní
  • Najít partnera
  • Stát se partnerem
O nás
  • Naše společnost
  • Podklady značky
  • Kontakujte nás
  • Práce
  • Události
  • Podcast
  • Blog
  • Zákazníci
  • Právní dokumenty • Soukromí
  • Zabezpečení
الْعَرَبيّة 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 je balíček open-source aplikací, které pokrývají všechny potřeby vaší společnosti: CRM, e-shop, účetnictví, sklady, kasy, projektové řízení a další.

Unikátní nabídka od Odoo poskytuje velmi jednoduché uživatelské rozhraní a vše je integrované na jednom místě.

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