Overslaan naar inhoud
Odoo Menu
  • Aanmelden
  • Probeer het gratis
  • Apps
    Financiën
    • Boekhouding
    • Facturatie
    • Onkosten
    • Spreadsheet (BI)
    • Documenten
    • Ondertekenen
    Verkoop
    • CRM
    • Verkoop
    • Kassasysteem winkel
    • Kassasysteem Restaurant
    • Abonnementen
    • Verhuur
    Websites
    • Websitebouwer
    • E-commerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Bevoorradingsketen
    • Voorraad
    • Productie
    • PLM
    • Inkoop
    • Onderhoud
    • Kwaliteit
    Personeelsbeheer
    • Werknemers
    • Werving & Selectie
    • Verlof
    • Evaluaties
    • Aanbevelingen
    • Wagenpark
    Marketing
    • Social media Marketing
    • E-mailmarketing
    • SMS Marketing
    • Evenementen
    • Marketingautomatisering
    • Enquêtes
    Diensten
    • Project
    • Urenstaten
    • Buitendienst
    • Helpdesk
    • Planning
    • Afspraken
    Productiviteit
    • Chat
    • Goedkeuringen
    • IoT
    • VoIP
    • Kennis
    • WhatsApp
    Apps van derden Odoo Studio Odoo Cloud Platform
  • Bedrijfstakken
    Detailhandel
    • Boekhandel
    • kledingwinkel
    • Meubelzaak
    • Supermarkt
    • Bouwmarkt
    • Speelgoedwinkel
    Food & Hospitality
    • Bar en Pub
    • Restaurant
    • Fastfood
    • Gastenverblijf
    • Drankenhandelaar
    • Hotel
    Vastgoed
    • Makelaarskantoor
    • Architectenbureau
    • Bouw
    • Vastgoedbeheer
    • Tuinieren
    • Vereniging van eigenaren
    Consulting
    • Accountantskantoor
    • Odoo Partner
    • Marketingbureau
    • Advocatenkantoor
    • Talentenwerving
    • Audit & Certificering
    Productie
    • Textiel
    • Metaal
    • Meubels
    • Eten
    • Brewery
    • Relatiegeschenken
    Gezondheid & Fitness
    • Sportclub
    • Opticien
    • Fitnesscentrum
    • Wellness-medewerkers
    • Apotheek
    • Kapper
    Trades
    • Klusjesman
    • IT-hardware & support
    • Zonne-energiesystemen
    • Schoenmaker
    • Schoonmaakdiensten
    • HVAC-diensten
    Andere
    • Non-profit organisatie
    • Milieuagentschap
    • Verhuur van Billboards
    • Fotograaf
    • Fietsleasing
    • Softwareverkoper
    Browse all Industries
  • Community
    Leren
    • Tutorials
    • Documentatie
    • Certificeringen
    • Training
    • Blog
    • Podcast
    Versterk het onderwijs
    • Onderwijs- programma
    • Scale Up! Business Game
    • Bezoek Odoo
    Download de Software
    • Downloaden
    • Vergelijk edities
    • Releases
    Werk samen
    • Github
    • Forum
    • Evenementen
    • Vertalingen
    • Word een Partner
    • Services for Partners
    • Registreer je accountantskantoor
    Diensten
    • Vind een partner
    • Vind een boekhouder
    • Een adviseur ontmoeten
    • Implementatiediensten
    • Klantreferenties
    • Ondersteuning
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Vraag een demo aan
  • Prijzen
  • Help

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

  • CRM
  • e-Commerce
  • Boekhouding
  • Voorraad
  • PoS
  • Project
  • MRP
All apps
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Help

"Self" Record set is empty

Inschrijven

Ontvang een bericht wanneer er activiteit is op deze post

Deze vraag is gerapporteerd
pythonselfv17
4 Antwoorden
4053 Weergaven
Avatar
IRIG Events

Hello,

I am working on a module where I implement a special, simplified version of a task.

Ithis task model, there are a few standard task types and each of those types comes with a specific set of state stages. 

I have created a table where I can store task types and stage names. I have gotten it so that at the time of creation, the task gets assignedd a specific task type.

Now my issue is that I want to use a callable for the selection field which calls on self to finnd out it's own task type, search for the stages corresponding to this task type and assign them to the selection field.

the theory sounds great, but for some reason, when I call self, it appears to be empty. When I try to debug using the console print, I always get 'irig.task()' (the name of the model) but no records in the recordset. 

can anybody help me figure out what am I calling wrong?

state_test = fields.Selection(selection='get_states')
 
def get_states(self):
  ​sel = [
​ ​('1','One'), ​
​ ​('2','Two') ​
      ]
​print(self) ​
​for r in self:
    ​records = self.env['irig.state'].search([('task_type', 'like', 'gear')])
​## It should llok like this, but the r in self is empty:
.search([('task_type', 'like', r.task_type)])​
​ ​
​ ​for rec in records: ​
        ​sel.append((rec.task_type, rec.name) )
​ ​ ​print(sel) ​
​return sel

In the above snippet, I have tested individual parts and it all works, the only part that since self shows as empty, then 'r in self' is also empty.

Even after the record is created, when I navigate to it, the 'get_states' method is called, but self is still empty.


Any help will be greatly appreciated!

0
Avatar
Annuleer
Rithik Sandron

have you overridden the create method to call get_states()?

IRIG Events
Auteur

@Rithik Sandron
I just tried it, but it still didn't work.

Avatar
Dương Nguyễn
Beste antwoord

In your case use compute instead, because self will always empty when you write seletion="your method"

I have searched across odoo 17 code where they have selection="method" and they do not use self just use something like self.env to search for other module

0
Avatar
Annuleer
Avatar
IRIG Events
Auteur Beste antwoord

O.k. For some reason I cant comment on anybody elses answers or comments. but after trying and playing with this, I can confirm that on the "selection" field, although it can use a callable function to popullate the options, there is no "self" available. so it is basically impossible to reference itself to choose the options based on some parameter of itself. 


However, I ended up solving my ultimate goal by using a Many2one relationship, with a domain based on a parameter of itself and then using the "statusbar" widget on it. Not the most ideal way, but it works. 


Thank you very much for your help and input.

 

-----------------------------------------------

Previous edit

@Rithik Sandron,

thank you very much for the example, however it doesnt really work.

the logic of getting the record after calling super(etc) works great in the sense that I indeed get a record that I can work with.

The problem now is that If I leave the field

state_test = fields.Selection(selection='get_states')

then, the method gets called before I create the record and every time I look at a record, both moments there is no self.

When I call the method from the create method, although now I do get the correct records and I do have a task to get info out of, it doesnt automatically assign the result to the selection field, and when I try to "force" it by doing:

task.state_test = selection_fields

I get an error saying that it is not the right value, even though it is a list of tuples.

the same behaviour happens when I change the state_test field to this:

state_test = fields.Selection([], string="State Test)

The part I still dont understand is how come when I call pretty much any other method, self does exist all the time, except for only this one method

cheers!

0
Avatar
Annuleer
Rithik Sandron

I have updated my answer. Check out if that helps

IRIG Events
Auteur

Sadly it still didnt work. However I found out that the 'selection=callable' Method in the selection field doesnt pass 'self'. Dont know if this is a bug or intentional for some reason, but I just tried calling the same 'get_states' method from the seleection field and from a compute on both a selection and a char field and directly from the selection=callable method I never got self, but from the compute I do get self and I can get all the info... However, since the result is a list of tuples, neither the return of a compute nor a direct assignment work.

Thank you very much to both, I will try to go deeper into it tomorrow.

Avatar
Rithik Sandron
Beste antwoord

If you're overriding the create method, try this:

from odoo import api

state_test = fields.Selection([('1', 'One'),
('2', 'Two'),
('3', 'Three')], string='State Test')

@api.model_create_multi
def create(self, values_list):
​for value in values_list:
​ // here the record has not been created yet, so the self object does not contain the record. self.get_states() will not work
​
​ ​line = super(model_class_name, self).create(vals)
​ // ​here the record has been created. the record object in returned to line
​ ​line.get_states() // this will work
​return line

def get_states(self):
​for record in self:
​ ​record.state_test = '2'

To change selection fields dynamically:

from odoo import models, fields, api


class IrigTask(models.Model):
​_name = "irig.task"
​_description = "irig.task"
​
​@api.model
def ​_get_selection_items(self):
​ sel = [('1', 'One'), ('2', 'Two')]​
​ ​return sel

​state_test = fields.Selection(selection=lambda self: self.env['irig.task']._get_selection_items(), string="State Test")

hope this helps. let me know


0
Avatar
Annuleer
Avatar
Cybrosys Techno Solutions Pvt.Ltd
Beste antwoord

Hi,

Please try to use api.model decorator before the function starts, and maybe in the self you may be getting a False value, If you are getting False in the print, then you can search the value.

example:


@api.model

def get_states(self):

      sel = [

        ('1','One'), ('2','Two') ]

         print(self)

         for r in self.env['model.name'].search([]):

         //your balance code



Hope it helps

-1
Avatar
Annuleer
Dương Nguyễn

Wrong purpose of using api.model, learn odoo again please.
When using api.model, we will not take into account value in self which mean we won't use self.'field' at al

Geniet je van het gesprek? Blijf niet alleen lezen, doe ook mee!

Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!

Aanmelden
Gerelateerde posts Antwoorden Weergaven Activiteit
i am using odoo 17. i have a one field 'wage' and it is a salary like number 1254200. i want comma in this number using Indian standard number comma format for example 12,54,200.00 how can i using python ?
python v17
Avatar
Avatar
1
mrt. 24
1694
Call method using XML RPC
python XML-RPC v17
Avatar
Avatar
1
dec. 23
3401
Iteration on self
python self odoo16features
Avatar
Avatar
2
jan. 23
3418
Create productvariant and add order
python product.product order.line v17
Avatar
Avatar
1
jul. 24
2331
Field used in docstring domain not present in view
python xml odoo16features v17
Avatar
Avatar
1
okt. 24
2418
Community
  • Tutorials
  • Documentatie
  • Forum
Open Source
  • Downloaden
  • Github
  • Runbot
  • Vertalingen
Diensten
  • Odoo.sh Hosting
  • Ondersteuning
  • Upgrade
  • Gepersonaliseerde ontwikkelingen
  • Onderwijs
  • Vind een boekhouder
  • Vind een partner
  • Word een Partner
Over ons
  • Ons bedrijf
  • Merkelementen
  • Neem contact met ons op
  • Vacatures
  • Evenementen
  • Podcast
  • Blog
  • Klanten
  • Juridisch • Privacy
  • Beveiliging
الْعَرَبيّة 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 een suite van open source zakelijke apps die aan al je bedrijfsbehoeften voldoet: CRM, E-commerce, boekhouding, inventaris, kassasysteem, projectbeheer, enz.

Odoo's unieke waardepropositie is om tegelijkertijd zeer gebruiksvriendelijk en volledig geïntegreerd te zijn.

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