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

Use a domain filter on many2one field

Subscribe

Get notified when there's activity on this post

This question has been flagged
filterdomainmany2one
3 Replies
72807 Views
Avatar
Emanuel Cino

I am new to OpenERP (v7) and am writing a module that extends the res.partner class and added the following two fields :

_columns = {
    'member_ids': fields.one2many('res.partner', 'church_id', 'Members', domain=[('active','=',True)]),
    'church_id': fields.many2one('res.partner', 'Church', domain="[('is_company','=',True)]")
}

What I would like to do, is that when the user opens the church_id view, it shows only partners that are churches. For now, it displays all companies as I am not able to set correctly the domain. A church is a company that has the category id (res.partner.category) corresponding to the church category. I tried to solve my problem using a functional field, but I am only getting some errors. The following function returns correctly a dictionary containing the current user id and the list of the church ids :

# Returns : {'user_id': [church_id1, church_id2, ...]}
def _get_church_ids(self, cr, uid, ids, field_name, arg, context=None):
    sql_req = """
        SELECT R.partner_id 
        FROM res_partner_res_partner_category_rel R
            LEFT JOIN res_partner_category C ON ( R.category_id = C.id)
        WHERE C.active = TRUE AND UPPER(C.name) = 'CHURCH'
    """
    cr.execute(sql_req)
    sql_res = cr.fetchall()
    return dict.fromkeys(ids, sql_res)

Corresponding field and view :

'church_ids': fields.function(_get_church_ids, type="one2many", relation='res.partner', method=True)
<field name="church_ids" invisible="1"/>

I tried the following domains on the view church_id, and I always get the same error :

Uncaught Error: Expected "]", got "(name)"

<field name="church_id" attrs="{'invisible': [('is_company','=',True)]} domain="[('id','in',[church for church in church_ids[id]])]"/>
<field name="church_id" attrs="{'invisible': [('is_company','=',True)]} domain="[('id','in',[church[0] for church in church_ids)]"/>

Any suggestions on how to do this ? I spent already some days trying to figure it out but with no luck. I also tried to do it with a related field but I couldn't understand how to achieve it... Many thanks for your help !

4
Avatar
Discard
Sehrish

Types of domain in many2one: https://learnopenerp.blogspot.com/2020/09/domain-filter-many2one-field-odoo13.html

Avatar
Eric
Best Answer

Your church_id field sounds like it should be related to a church table, but I think you just want to indicate if a partner record is a church or not. I would just create a field called 'is_church' as a boolean, then you can use a domain to filter for any partner records where the is_church value is set to true like this

domain = "[('is_church','=',True)]"

You can get rid of the church_id field because it's not related to a church table. I believe that should work for you. Hope it helps

in your .py file:

_columns{
    'is_church': fields.boolean('Is a Church', domain="[('is_church', '=', True)]")
}
4
Avatar
Discard
Emanuel Cino
Author

You're right, I was doing it unnecessarily hard. That's far better so !

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
How not to show currently logged in employee in many2one field drop-down list? Solved
filter domain many2one
Avatar
Avatar
1
Sep 19
3874
How to dynamically update domain filter on selection widget?
filter domain many2one selection
Avatar
Avatar
Avatar
2
Sep 23
10482
Filter many2one field to not include already used values
filter domain many2one odoo
Avatar
1
Apr 22
3261
Many2One Domain Solved
filter domain many2one workorder
Avatar
Avatar
1
Jul 20
6300
During a record creation, how to get the values of table1 to filter table2's many2one?
v6.1 filter domain many2one
Avatar
Avatar
2
Mar 15
7104
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