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č

can not get the context value in my function field_view_get

Naroči se

Get notified when there's activity on this post

This question has been flagged
1 Odgovori
1211 Prikazi
Avatar
mokhtar

hello all,

Please help overcome this issue I can not get context value 

@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
# Call the super method to get the base view structure
res = super(HrEmployeePrivate, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar,
submenu=submenu)

if view_type == 'form':
# Ensure the branch category is correctly set in the context
rec = self.env['flex.branch'].search([('id', 'in', self.branch_name_id.ids)]).ids
branch_cate = self.branch_name_id.branch_cate if self.branch_name_id else "tttttt"
# Add branch category to the context
print('the value is :', rec)
self = self.sudo().with_context(bra_class=branch_cate)
# Retrieve the value from the context
bra_class_value = self._context.get('bra_class', 'لايوجد') # Default to 'لايوجد' if not found
print("Context Value for 'bra_class':", bra_class_value)

# Iterate through fields in the view
for field_name, field_data in res.get('fields', {}).items():
if field_name == 'job_ids':
# Dynamically set the domain for the field
field_data['domain'] = [
'|',
('company_id', '=', False),
('company_id', '=', self.env.user.company_id.id),
('bra_class', '=', bra_class_value) # Use the context value dynamically
]
return res
0
Avatar
Opusti
Avatar
Mohammad Al Khatib
Best Answer

our issue seems to stem from how the context is being set and accessed in your custom Odoo method. Let’s break it down and identify why the context value (bra_class) might not be retrieved as expected.

Issues in Your Code:

1. Context Setting on self:

  • In this line:
self = self.sudo().with_context(bra_class=branch_cate)

You’re modifying self to include the context, but this doesn’t actually persist the new context to the method. The self reference is local to this method and does not affect the surrounding environment or the call stack.

2. Accessing Context with self._context:

  • Later in the method, when you attempt to retrieve the value using:

bra_class_value = self._context.get('bra_class', 'لايوجد')

The _context attribute on self refers to the original context of the method call, not the modified context you attempted to set with with_context.

3. Improper Use of with_context:

  • The with_context method returns a new recordset with the added context, but it does not retroactively apply that context to the method already in progress.


Suggested Fix:

Modify your approach to work directly with the context passed to the fields_view_get method. Here’s the corrected code:

@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
    # Call the super method to get the base view structure
    res = super(HrEmployeePrivate, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar,
                                                         submenu=submenu)

    if view_type == 'form':
        # Get branch category and add it to the context
        branch_cate = self.branch_name_id.branch_cate if self.branch_name_id else "tttttt"

        # Add branch category to the context
        ctx = dict(self._context, bra_class=branch_cate)

        # Retrieve the value from the updated context
        bra_class_value = ctx.get('bra_class', 'لايوجد')  # Default to 'لايوجد' if not found
        print("Context Value for 'bra_class':", bra_class_value)

        # Iterate through fields in the view
        for field_name, field_data in res.get('fields', {}).items():
            if field_name == 'job_ids':
                # Dynamically set the domain for the field
                field_data['domain'] = [
                    '|',
                    ('company_id', '=', False),
                    ('company_id', '=', self.env.user.company_id.id),
                    ('bra_class', '=', bra_class_value)  # Use the context value dynamically
                ]
    return res

Key Changes:

1. ctx = dict(self._context, bra_class=branch_cate)

  • This creates a new dictionary by copying the current context and adding the bra_class key.

2. Access Context via the Updated ctx:

  • Instead of attempting to modify self, you work with the ctx variable and extract bra_class from it.

3. Use ctx for Further Logic:

  • The updated context is used to dynamically set the domain for job_ids.

Debugging Tips:

1. Ensure branch_name_id is Set:

  • Add a check to verify that branch_name_id is populated; otherwise, branch_cate will default to “tttttt”.

2. Log the Context:

  • Use print or logging to confirm the structure of the context before and after modifications.

3. Validate Domains:

  • Test the resulting domain to ensure it’s applied correctly in the form view.


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