Skip to Content
Odoo Menu
  • Sign in
  • 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
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

How to filter an ManytoOne field using domain filter.

Subscribe

Get notified when there's activity on this post

This question has been flagged
many2onedomain_filterfilteringdropdownlistodoo17
2 Replies
1515 Views
Avatar
imrantc

Hi all, 

i have 2 models , 

Passport and Employees

class PassportManagementModels(models.Model):    
_name = 'om_hrm.passport'
_description = 'Passport Master'
_inherit = ['mail.thread']
_rec_name = "passport_no"
passport_no = fields.Char(string='Passport Number',size=16,required=True) PassportIssueDate = fields.Date(string='Passport Issue Date', required=True) PassportExpiryDate =fields.Date(string="Expiry Date",required=True) PassportNationality =fields.Char(string="Nationality",size=20,required=True) Passportdob = fields.Date(string="Date of Birth",required=True)
Passportholdername = fields.Char(string="Holder Full Name",required=True) Passportpossesionwith = fields.Selection([("admin","Admin"),("staff","Staff"),("other","Other")],string="Possesion With!")
passport_doc_ids = fields.Many2many('ir.attachment',string='Passport Documents',ondelete='cascade')
_sql_constraints = [        ('key_uniq', 'unique (passport_no)', 'PassportNo must be unique.')    ]
class EmployeesManagementModels(models.Model):    
_name = 'om_hrm.employee' _description = 'Employee Master'
_inherit = ['mail.thread']
_rec_name = "full_Name"
employeeId = fields.Char(string='Employee id',required= True)
passport_id = fields.Many2one('om_hrm.passport', string='Passport', ondelete='set null',domain=lambda self:[('id','=',self._get_assigned_passport_ids())])
@api.onchange('passport_id')
def onchange_passport_id(self): # Refresh the domain for passport_id to show only unassigned passports
self._get_assigned_passport_ids()

@api.model
def _get_assigned_passport_ids(self): # Get a list of all passport IDs already assigned to any employee
allemployees = self.search([])
print("Employees list:",allemployees)
assigned_passport_ids = self.search([]).mapped('passport_id.id') print("Assigned Passport IDs:", assigned_passport_ids)
return assigned_passport_ids

i want to see the passports which are not assigned to any employees,like fetching all passport and checking if the employee table contains the id then it shouldnt show in the dropdown list.

Any help is appreciated, thanks.

0
Avatar
Discard
Avatar
Cybrosys Techno Solutions Pvt.Ltd
Best Answer

Hi,

Please check the below code


Python

class EmployeesManagementModels(models.Model):    
_name = 'om_hrm.employee'
_description = 'Employee Master'
_inherit = ['mail.thread']

employeeId = fields.Char(string='Employee id',required= True)
assigned_passport_ids = fields.Many2many(
'om_hrm.passport', string="Assigned Passport",
compute="_compute_assigned_passport_ids")
passport_id = fields.Many2one(
'om_hrm.passport', string='Passport',
domain="[('id', 'not in', assigned_passport_ids)]")

@api.depends('passport_id')
def _compute_assigned_passport_ids(self):
for rec in self:
assigned_passport_ids = self.search([]).mapped('passport_id.id')
rec.assigned_passport_ids = assigned_passport_ids

XML


<record id="om_hrm_employee_view_form" model="ir.ui.view">
<field name="name">om_hrm.employee.view.form</field>
<field name="model">om_hrm.employee</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="employeeId"/>
<field name="assigned_passport_ids"
widget="many2many_tags" invisible="1"/>
<field name="passport_id"/>
</group>
</sheet>
<chatter/>
</form>
</field>
</record>

Hope it helps.

0
Avatar
Discard
Avatar
Manish Bohra
Best Answer

Hello, 

There are multiple solution but you can do one thing add one employee field in 'om_hrm.passport' table. 

When you link any employee with passport in case you map the employee. 

After you can do one thing in case of domain apply condition domain=[('employee_id','=',False)]

Thanks.

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

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

Sign up
Related Posts Replies Views Activity
Domain Filtering help opposite of child_of opperator Solved
domain_filter filtering
Avatar
Avatar
1
Mar 15
14631
How can I dynamically filter many2one fields in Odoo 17 models? Solved
many2one domain_filter dynamic filter
Avatar
Avatar
Avatar
Avatar
3
Aug 25
3378
Filter product in order line using code
filtering order.lines odoo17
Avatar
Avatar
1
Sep 24
1209
How to remove 'Create' and 'Edit' in the Many2one field of all models? Solved
views many2one odoo17
Avatar
Avatar
Avatar
3
Jul 24
4744
how to load one to many field in pos session odoo17?
pos many2one odoo17
Avatar
0
Jun 24
70
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