Skip to Content
Odoo Meniu
  • Autentificare
  • Try it free
  • Aplicații
    Finanțe
    • Contabilitate
    • Facturare
    • Cheltuieli
    • Spreadsheet (BI)
    • Documente
    • Semn
    Vânzări
    • CRM
    • Vânzări
    • POS Shop
    • POS Restaurant
    • Abonamente
    • Închiriere
    Site-uri web
    • Constructor de site-uri
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Lanț Aprovizionare
    • Inventar
    • Producție
    • PLM
    • Achiziție
    • Maintenance
    • Calitate
    Resurse Umane
    • Angajați
    • Recrutare
    • Time Off
    • Evaluări
    • Referințe
    • Flotă
    Marketing
    • Social Marketing
    • Marketing prin email
    • SMS Marketing
    • Evenimente
    • Automatizare marketing
    • Sondaje
    Servicii
    • Proiect
    • Foi de pontaj
    • Servicii de teren
    • Centru de asistență
    • Planificare
    • Programări
    Productivitate
    • Discuss
    • Aprobări
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Aplicații Terțe Odoo Studio Platforma Odoo Cloud
  • Industrii
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Managament
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Producție
    • Textile
    • Metal
    • Furnitures
    • Food
    • 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
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Comunitate
    Învăță
    • Tutorials
    • Documentație
    • Certificări
    • Instruire
    • Blog
    • Podcast
    Empower Education
    • Program Educațional
    • Scale Up! Business Game
    • Visit Odoo
    Obține Software-ul
    • Descărcare
    • Compară Edițiile
    • Lansări
    Colaborați
    • Github
    • Forum
    • Evenimente
    • Translations
    • Devino Partener
    • Services for Partners
    • Înregistrează-ți Firma de Contabilitate
    Obține Servicii
    • Găsește un Partener
    • Găsiți un contabil
    • Meet an advisor
    • Servicii de Implementare
    • Referințe ale clienților
    • Suport
    • Actualizări
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Obține un demo
  • Prețuri
  • Ajutor

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

  • CRM
  • e-Commerce
  • Contabilitate
  • Inventar
  • PoS
  • Proiect
  • MRP
All apps
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
All Posts Oameni Insigne
Etichete (View all)
odoo accounting v14 pos v15
Despre acest forum
Suport

Issue: Return dynamic domain for field with on change method In Odoo 17

Abonare

Primiți o notificare când există activitate la acestă postare

Această întrebare a fost marcată
domainonchangeodoo17
7 Răspunsuri
14049 Vizualizări
Imagine profil
Long Duong Nhat
I'm having a problem with returning the domain for the field when executing onchange method.
For the code below, it works fine on odoo version 14. But on odoo version 17, it doesn't work. Can you guys give me a solution to do this?
I want the service_request_ids field to display only according to the selected service_id.

service_id = fields.Many2one('ahamove.service', string='Service')
service_request_ids = fields.Many2many('ahamove.service.request', 'estimate_request_rel','choose_id', 'request_id', string='Request Type')

@api.onchange('service_id')
def _onchange_service_id(self):
for rec in self:
if rec.service_id:
return {'domain': {'service_request_ids': [('service_id', '=', rec.service_id.id)]}}
1
Imagine profil
Abandonează
Imagine profil
Kiran K
Cel mai bun răspuns

Hi  Long Duong Nhat,

@api.onchange functions that return a domain have been deprecated from v17.

Check this alternative method,
https://github.com/odoo/odoo/blob/26239b2d0bdbc2f06d50f7739d61c490248b65bb/addons/account/models/account_tax.py#L1633C5-L1633C154


3
Imagine profil
Abandonează
Long Duong Nhat
Autor

Hi Kiran K, thank you for your response. I will try your solution now.

Imagine profil
Long Duong Nhat
Autor Cel mai bun răspuns

Hi Dương Nguyễn, Kiran K,  Cybrosys Techno Solutions Pvt.Ltd,
Thank you very much for assisting me with different solutions. 
After testing all the solutions, I solved the problem most suitably (By compiling your solutions).

class ChooseDeliveryCarrier(models.TransientModel):
_inherit = 'choose.delivery.carrier'

​service_id = fields.Many2one('ahamove.service', string='Service')
​service_request_domain = fields.Binary(default=[])
​service_request_ids = fields.Many2many('ahamove.service.request', ​'estimate_request_rel','choose_id', 'request_id', string='Request Type')

@api.onchange('service_id')
​def _onchange_service_id(self):
​ ​for rec in self:
​ ​if rec.service_id:
​ ​rec.service_request_domain = [('service_id', '=', rec.service_id.id)]

widget="many2many_tags"
options="{'no_create': True}"
invisible="delivery_type != 'ahamove'"
domain="service_request_domain"
/>

Best regards,
Long Duong Nhat


1
Imagine profil
Abandonează
Imagine profil
Cybrosys Techno Solutions Pvt.Ltd
Cel mai bun răspuns

Hi,


You can use the compute function instead like this:

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

service_request_ids = fields.Many2many('ahamove.service.request', 'estimate_request_rel',

'choose_id', 'request_id', string='Request Type',                       compute='_compute_service_request_ids', store=True)

@api.depends('service_id')

def _compute_service_request_ids(self):

for rec in self:

if rec.service_id:

rec.service_request_ids = self.env['ahamove.service.request'].search([('service_id', '=', rec.service_id.id)])


Hope it helps


1
Imagine profil
Abandonează
Long Duong Nhat
Autor

Hi Cybrosys, thank you for the solution. It seems to work, the service_request_ids are all loaded according to the correct service_id. However, service_request_ids is not selectable (it's read-only).

Imagine profil
Dương Nguyễn
Cel mai bun răspuns

Does adding domain in field definition not work ?

service_request_ids = fields.Many2many(domain="[('service_id', '=', service_id)]")

This assume that service_request_ids containt service_id in it as m2o field

1
Imagine profil
Abandonează
Long Duong Nhat
Autor

Hi Duong Nguyen, thank you for your response.
It (domain="[('service_id', '=', service_id)]") doesn't work. When I select it, it cannot load the record

Long Duong Nhat
Autor

The relationship between service_id and service_request_ids are One2Many.
class AhamoveService(models.Model):
_name = 'ahamove.service'
request_ids = fields.One2many('ahamove.service.request', 'service_id', 'Requests')

class AhamoveServiceRequest(models.Model):
_name = 'ahamove.service.request'
service_id = fields.Many2one('ahamove.service', string='Service')

Dương Nguyễn

Which mean when user choose service_id on form view it will automatically load all related record of 'ahamove.service.request' which have that 'service_id'
If that so, i suggest using compute store with readonly=False

Long Duong Nhat
Autor

Yes, that's right,
But as far as I understand,
domain="[('service_id', '=', service_id)]" and compute method is a form of the static domain. This means that every time you access the form, it will work as expected. But when you onchange service_id it will not work. I have tried doing this.

Dương Nguyễn

No, i haven't seen any one implement onchange method like you did.
Normally , when implement onchange , it will be used to change some other field value and the most important thing of onchange is not store on the database level.
So i guest u should remove your onchange method and do something like this
@api.depends('service_id')
def _compute_request_ids(self):
all_ahamove_service_request = self.env["ahamove.service.request"].search([("service_id", "in", self.service_id.ids)])
for r in self:
if r.service_id:
r.request_ids = all_ahamove_service_request.filtered(lambda req: req.service_id == r.service_id )
else:
r.request_ids = False

Long Duong Nhat
Autor

Thanks Duong, I will try your solution now.

Imagine profil
Adil Akbar
Cel mai bun răspuns

Hi, you can follow this: https://youtu.be/f3mq5fHRPT0

Hope it helps, Thanks

0
Imagine profil
Abandonează
Long Duong Nhat
Autor

Thanks so much, Adil Akbar

Imagine profil
Parth Tilokani
Cel mai bun răspuns

Why are you using binary field to store domain and do you think in xml it will render it in correct format and work? I have not tried the solution but I am curious about why take a binary field?

​service_request_domain = fields.Binary(default=[])
0
Imagine profil
Abandonează
Imagine profil
Nicolás
Cel mai bun răspuns

Hi Long,
Thank you so much. I've been searching for days on how to implement this in Odoo 17.
I only added this to my "service_requests_ids" field in the XML view:

widget="many2many_tags"
options="{'no_create': True}"
domain="service_request_domain"

It is important to note that the "service_request_domain" must be included in the XML view file and set to invisible.

Best regards,
Nicolás

0
Imagine profil
Abandonează
Long Duong Nhat
Autor

Hi Nicolás,
#python-code
class ChooseDeliveryCarrier(models.TransientModel):
_inherit = 'choose.delivery.carrier'

​service_id = fields.Many2one('ahamove.service', string='Service')
​service_request_domain = fields.Binary(default=[])
​service_request_ids = fields.Many2many('ahamove.service.request', ​'estimate_request_rel','choose_id', 'request_id', string='Request Type')

@api.onchange('service_id')
​def _onchange_service_id(self):
​ ​for rec in self:
​ ​if rec.service_id:
​ ​rec.service_request_domain = [('service_id', '=', rec.service_id.id)]

#xml-code
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_choose_delivery_carrier_form_with_provider" model="ir.ui.view">
<field name="name">view.choose.delivery.carrier.form.with.provider</field>
<field name="model">choose.delivery.carrier</field>
<field name="inherit_id" ref="delivery.choose_delivery_carrier_view_form" />
<field name="arch" type="xml">
<field name="carrier_id" position="after">
<field name="service_id"/>
<field name="service_request_ids" widget="many2many_tags" domain="service_request_domain"/>
<field name="service_request_domain" invisible="1"/>
</field>
</field>
</record>
</odoo>

Thanks and best regards,

Enjoying the discussion? Don't just read, join in!

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

Înscrie-te
Related Posts Răspunsuri Vizualizări Activitate
How i can Apply onchange method on one2many field ?
domain onchange
Imagine profil
Imagine profil
1
sept. 19
5188
Dynamic domain with Onchange doesn't show value when creating
domain onchange domain_filter
Imagine profil
Imagine profil
Imagine profil
Imagine profil
3
mai 24
6491
How to return a domain on field on onchange action ? Rezolvat
domain onchange context
Imagine profil
Imagine profil
Imagine profil
Imagine profil
3
nov. 24
46451
How to return domain on one2many field so that seleted values are not shown in next line
domain one2many onchange
Imagine profil
1
apr. 23
5931
onchange return domain for one2many product ID Field Rezolvat
domain one2many onchange
Imagine profil
Imagine profil
Imagine profil
Imagine profil
7
apr. 23
19881
Comunitate
  • Tutorials
  • Documentație
  • Forum
Open Source
  • Descărcare
  • Github
  • Runbot
  • Translations
Servicii
  • Hosting Odoo.sh
  • Suport
  • Actualizare
  • Custom Developments
  • Educație
  • Găsiți un contabil
  • Găsește un Partener
  • Devino Partener
Despre Noi
  • Compania noastră
  • Active de marcă
  • Contactați-ne
  • Locuri de muncă
  • Evenimente
  • Podcast
  • Blog
  • Clienți
  • Aspecte juridice • Confidențialitate
  • Securitate
الْعَرَبيّة 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 este o suită de aplicații de afaceri open source care acoperă toate nevoile companiei dvs.: CRM, comerț electronic, contabilitate, inventar, punct de vânzare, management de proiect etc.

Propunerea de valoare unică a Odoo este să fie în același timp foarte ușor de utilizat și complet integrat.

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