Skip to Content
Odoo Menu
  • Prijavi
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    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 Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & 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
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

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č

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

Naroči se

Get notified when there's activity on this post

This question has been flagged
domainonchangeodoo17
7 Odgovori
14010 Prikazi
Avatar
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
Avatar
Opusti
Avatar
Kiran K
Best Answer

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
Avatar
Opusti
Long Duong Nhat
Avtor

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

Avatar
Long Duong Nhat
Avtor Best Answer

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
Avatar
Opusti
Avatar
Cybrosys Techno Solutions Pvt.Ltd
Best Answer

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
Avatar
Opusti
Long Duong Nhat
Avtor

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).

Avatar
Dương Nguyễn
Best Answer

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
Avatar
Opusti
Long Duong Nhat
Avtor

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
Avtor

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
Avtor

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
Avtor

Thanks Duong, I will try your solution now.

Avatar
Adil Akbar
Best Answer

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

Hope it helps, Thanks

0
Avatar
Opusti
Long Duong Nhat
Avtor

Thanks so much, Adil Akbar

Avatar
Parth Tilokani
Best Answer

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
Avatar
Opusti
Avatar
Nicolás
Best Answer

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
Avatar
Opusti
Long Duong Nhat
Avtor

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!

Prijavi
Related Posts Odgovori Prikazi Aktivnost
How i can Apply onchange method on one2many field ?
domain onchange
Avatar
Avatar
1
sep. 19
5175
Dynamic domain with Onchange doesn't show value when creating
domain onchange domain_filter
Avatar
Avatar
Avatar
Avatar
3
maj 24
6483
How to return a domain on field on onchange action ? Solved
domain onchange context
Avatar
Avatar
Avatar
Avatar
3
nov. 24
46441
How to return domain on one2many field so that seleted values are not shown in next line
domain one2many onchange
Avatar
1
apr. 23
5921
onchange return domain for one2many product ID Field Solved
domain one2many onchange
Avatar
Avatar
Avatar
Avatar
7
apr. 23
19873
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة 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