콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
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.
아바타
취소
베스트 답변

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

아바타
취소
베스트 답변

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.

아바타
취소
베스트 답변

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

아바타
취소
관련 게시물 답글 화면 활동
2
11월 22
4737
3
11월 20
4199
1
11월 18
5689
5
2월 18
4809
2
7월 22
11718