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č

How can I dynamically filter many2one fields in Odoo 17 models?

Naroči se

Get notified when there's activity on this post

This question has been flagged
many2onedomain_filterdynamic filter
3 Odgovori
3034 Prikazi
Avatar
Bahrom Najmiddinov
class OrderPatient(models.Model):
_name = 'hms.order.patient'
_description = 'Order Patient'

order_id = fields.Many2one('hms.order', invisible=True, readonly=True)
service_id = fields.Many2one('hms.service', string="Service", domain='_onchange_service_id_domain')
doctor_id = fields.Many2one('hr.employee', string="Doctor")

# service_id_domain = fields.Char(compute='_compute_service_id_domain', store=False)
# doctor_id_domain = fields.Char(compute='_compute_doctor_id_domain', store=False)

@api.onchange('doctor_id')
def _onchange_doctor_id(self):
"""Update services and clear invalid service"""
if self.doctor_id:
allowed_services = self.doctor_id.service_ids
# Clear service if not valid for new doctor
if self.service_id not in allowed_services:
self.service_id = False
self.service_id_domain = [('id', 'in', allowed_services.ids)]
return {'domain': {'service_id': [('id', 'in', allowed_services.ids)]}}
else:
self.service_id = False
self.service_id_domain = []
return {'domain': {'service_id': []}}

@api.onchange('service_id')
def _onchange_service_id(self):
"""Update doctors and clear invalid doctor"""
if self.service_id:
allowed_doctors = self.env['hr.employee'].search(
[('service_ids', 'in', self.service_id.ids)]
)
# Clear doctor if not valid for new service
if self.doctor_id not in allowed_doctors:
self.doctor_id = False
self.doctor_id_domain = {'domain': {'doctor_id': [('id', 'in', allowed_doctors.ids)]}}
return {'domain': {'doctor_id': [('id', 'in', allowed_doctors.ids)]}}
else:
self.doctor_id = False
self.doctor_id_domain = {'domain': {'doctor_id': []}}
return {'domain': {'doctor_id': []}}


I want to achieve the following behavior:

  1. When a Doctor is selected, only the services that are allowed for that Doctor should be available for selection in the Service field. If the selected service is no longer valid for the Doctor, it should be cleared.
  2. When a Service is selected, only the Doctors that are allowed to perform that Service should be available for selection in the Doctor field. If the selected Doctor no longer provides the selected Service, it should be cleared.
0
Avatar
Opusti
Avatar
Cybrosys Techno Solutions Pvt.Ltd
Best Answer

Hi,

Please refer to the code below:


class OrderPatient(models.Model):

    _name = 'hms.order.patient'

    _description = 'Order Patient'


    order_id = fields.Many2one('hms.order', invisible=True, readonly=True)

    service_id = fields.Many2one(

        'hms.service',

        string="Service",

        domain="[('id', 'in', available_service_ids)]"

    )

    doctor_id = fields.Many2one(

        'hr.employee',

        string="Doctor",

        domain="[('id', 'in', available_doctor_ids)]"

    )

    available_service_ids = fields.Many2many(

        'hms.service', compute='_compute_available_service_ids', string="Available Services"

    )

    available_doctor_ids = fields.Many2many(

        'hr.employee', compute='_compute_available_doctor_ids', string="Available Doctors"

    )


    @api.depends('doctor_id')

    def _compute_available_service_ids(self):

        for record in self:

            if record.doctor_id:

                record.available_service_ids = record.doctor_id.service_ids

                if record.service_id and record.service_id not in record.available_service_ids:

                    record.service_id = False

            else:

                record.available_service_ids = self.env['hms.service'].browse([])

                record.service_id = False


    @api.depends('service_id')

    def _compute_available_doctor_ids(self):

        for record in self:

            if record.service_id:

                allowed_doctors = self.env['hr.employee'].search([

                    ('service_ids', 'in', record.service_id.id)

                ])

                record.available_doctor_ids = allowed_doctors

                if record.doctor_id and record.doctor_id not in record.available_doctor_ids:

                    record.doctor_id = False

            else:

                record.available_doctor_ids = self.env['hr.employee'].browse([])

                record.doctor_id = False


Hope it helps.

0
Avatar
Opusti
Avatar
Dino Varghese
Best Answer

Hi Bahrom Najmiddinov,

Returning a domain from the @api.onchange​  method is deprecated in Odoo 17. See the link below for the recommended alternative.
https://github.com/odoo/odoo/blob/26239b2d0bdbc2f06d50f7739d61c490248b65bb/addons/account/models/account_tax.py#L1633C5-L1633C154

1
Avatar
Opusti
Bahrom Najmiddinov
Avtor

thanks

Avatar
D Enterprise
Best Answer

Hi,

please try this code 

from odoo import models, fields, api


class OrderPatient(models.Model):

    _name = 'hms.order.patient'

    _description = 'Order Patient'


    order_id = fields.Many2one('hms.order', invisible=True, readonly=True)

    service_id = fields.Many2one('hms.service', string="Service")

    doctor_id = fields.Many2one('hr.employee', string="Doctor")


    @api.onchange('doctor_id')

    def _onchange_doctor_id(self):

        """Filter services by selected doctor"""

        if self.doctor_id:

            allowed_services = self.doctor_id.service_ids

            if self.service_id not in allowed_services:

                self.service_id = False

            return {

                'domain': {

                    'service_id': [('id', 'in', allowed_services.ids)],

                }

            }

        else:

            self.service_id = False

            return {'domain': {'service_id': []}}


    @api.onchange('service_id')

    def _onchange_service_id(self):

        """Filter doctors by selected service"""

        if self.service_id:

            allowed_doctors = self.env['hr.employee'].search([

                ('service_ids', 'in', self.service_id.id )

            ])

            if self.doctor_id not in allowed_doctors:

                self.doctor_id = False

            return {

                'domain': {

                    'doctor_id': [('id', 'in', allowed_doctors.ids)],

                }

            }

        else:

            self.doctor_id = False

            return {'domain': {'doctor_id': []}}


i hope it is usefull

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
Related Posts Odgovori Prikazi Aktivnost
Odoo 13 many2one filter base of the current user Solved
many2one list domain_filter
Avatar
Avatar
Avatar
2
nov. 22
5336
Filter records in Many2One by records parents ID?
many2one domain_filter odoo12
Avatar
3
nov. 20
4743
How to filter the domain of a selectbox depending on the value of another selectbox Solved
many2one domain_filter odoo11
Avatar
Avatar
1
nov. 18
6428
Domain filter odoo11 manyToOne
many2one domain_filter odoo11
Avatar
5
feb. 18
5413
How to give Domain filter for one2many field base on the condition of another field? (Odoo 13) Solved
many2one one2many onchange domain_filter
Avatar
Avatar
2
jul. 22
12684
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