Skip to Content
Odoo Menu
  • Prihlásiť sa
  • Vyskúšajte zadarmo
  • Aplikácie
    Financie
    • Účtovníctvo
    • Fakturácia
    • Výdavky
    • Tabuľka (BI)
    • Dokumenty
    • Podpis
    Predaj
    • CRM
    • Predaj
    • POS Shop
    • POS Restaurant
    • Manažment odberu
    • Požičovňa
    Webstránky
    • Tvorca webstránok
    • eShop
    • Blog
    • Fórum
    • Živý chat
    • eLearning
    Supply Chain
    • Sklad
    • Výroba
    • Správa životného cyklu produktu
    • Nákup
    • Údržba
    • Manažment kvality
    Ľudské zdroje
    • Zamestnanci
    • Nábor zamestnancov
    • Voľné dni
    • Hodnotenia
    • Odporúčania
    • Vozový park
    Marketing
    • Marketing sociálnych sietí
    • Email marketing
    • SMS marketing
    • Eventy
    • Marketingová automatizácia
    • Prieskumy
    Služby
    • Projektové riadenie
    • Pracovné výkazy
    • Práca v teréne
    • Helpdesk
    • Plánovanie
    • Schôdzky
    Produktivita
    • Tímová komunikácia
    • Schvalovania
    • IoT
    • VoIP
    • Znalosti
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Priemyselné odvetvia
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Reštaurácia
    • Fast Food
    • Guest House
    • Beverage distributor
    • Hotel
    Reality
    • Real Estate Agency
    • Architecture Firm
    • Konštrukcia
    • Estate Managament
    • Gardening
    • Property Owner Association
    Poradenstvo
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Výroba
    • Textile
    • Metal
    • Furnitures
    • Jedlo
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware and Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Iní
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Komunita
    Vzdelávanie
    • Tutoriály
    • Dokumentácia
    • Certifikácie
    • Školenie
    • Blog
    • Podcast
    Empower Education
    • Vzdelávací program
    • Scale Up! Business Game
    • Visit Odoo
    Softvér
    • Stiahnuť
    • Porovnanie Community a Enterprise vierzie
    • Releases
    Spolupráca
    • Github
    • Fórum
    • Eventy
    • Preklady
    • Staň sa partnerom
    • Services for Partners
    • Register your Accounting Firm
    Služby
    • Nájdite partnera
    • Nájdite účtovníka
    • Meet an advisor
    • Implementation Services
    • Zákaznícke referencie
    • Podpora
    • Upgrades
    ​Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Získajte demo
  • Cenník
  • Pomoc

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

  • CRM
  • e-Commerce
  • Účtovníctvo
  • Sklady
  • PoS
  • Projektové riadenie
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tagy (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tagy (View all)
odoo accounting v14 pos v15
About this forum
Pomoc

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

Odoberať

Get notified when there's activity on this post

This question has been flagged
many2onedomain_filterdynamic filter
3 Replies
3037 Zobrazenia
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
Zrušiť
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
Zrušiť
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
Zrušiť
Bahrom Najmiddinov
Autor

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
Zrušiť
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Registrácia
Related Posts Replies Zobrazenia Aktivita
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
4745
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
6429
Domain filter odoo11 manyToOne
many2one domain_filter odoo11
Avatar
5
feb 18
5416
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
júl 22
12685
Komunita
  • Tutoriály
  • Dokumentácia
  • Fórum
Open Source
  • Stiahnuť
  • Github
  • Runbot
  • Preklady
Služby
  • Odoo.sh hosting
  • Podpora
  • Vyššia verzia
  • Custom Developments
  • Vzdelávanie
  • Nájdite účtovníka
  • Nájdite partnera
  • Staň sa partnerom
O nás
  • Naša spoločnosť
  • Majetok značky
  • Kontaktujte nás
  • Pracovné ponuky
  • Eventy
  • Podcast
  • Blog
  • Zákazníci
  • Právne dokumenty • Súkromie
  • Bezpečnosť
الْعَرَبيّة 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 sada podnikových aplikácií s otvoreným zdrojovým kódom, ktoré pokrývajú všetky potreby vašej spoločnosti: CRM, e-shop, účtovníctvo, skladové hospodárstvo, miesto predaja, projektový manažment atď.

Odoo prináša vysokú pridanú hodnotu v jednoduchom použití a súčasne plne integrovanými biznis aplikáciami.

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