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

Python: Retrieving filtered records

Subscribe

Get notified when there's activity on this post

This question has been flagged
filterpythonv18
3 Replies
1913 Views
Avatar
Tessa Roberts

Hello,


Is there a way to retrieve, in python, the domain of active filters in the list view or the list of record IDs displayed in the list view?


I have a search function on a custom field and in this function, I would like to call a function on the IDs of the list of records displayed in the list view.


I've tried looking in the context but there's no parameter similar to “active_ids”.


Is this possible?


Thank you

0
Avatar
Discard
Christoph Farnleitner

First, great you took the time to updated your post with your findings! Apart from that, I'm unsure about your usecase. For doing something based on records visible I would usually rather consider a computed field that is not stored. Also, as said, unsure about your usecase, but search, read, read_group etc all are functions on odoo.models (like create, write, etc) and can be extended per model. The search function for example has this definition: def search(self, domain, offset=0, limit=None, order=None) -> Self

Avatar
Tessa Roberts
Author Best Answer

I've found a way to get the active domain.

However, I think there must still be a better way to solve the problem.


In the browser inspector, I studied the http requests sent and received when a filter was activated.

The request looks something like this:

{
"jsonrpc":"2.0",
"method":"call",
"params":{
"model":"crm.lead",
"domain":[["type","=","opportunity"],["date",">=","2019-04-18"]],
"fields":["partner_id","user_id","phone","categs_id","state",],
"limit":80,
"sort":"create_date DESC",
"context":{
"lang":"fr_FR",
"tz":"Europe/Paris",
"uid":31,
"allowed_company_ids":[1],
"params":{"action":1647,"model":"crm.lead","view_type":"list","cids":"","menu_id":451},
"stage_type":"opportunity",
"default_type":"opportunity",
"default_user_id":31,
"needaction_menu_ref":"sale.menu_sale_quotations",
"bin_size":true
}
},"id":393865456
}


I was able to recover the active domain by importing “request” from odoo.http


from odoo.http import request


And then, in the source code of my python function :


http_request_domain = request.params.get('domain')


Based on all this, I think I'll be able to retrieve the list of record ids corresponding to the domain and achieve my goal.


Have a nice day

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

Hi,


In Odoo 17, you can retrieve the active filters (domain) in a list view and also access the records currently visible using the following method.

from odoo import models, api

class YourModel(models.Model):

    _inherit = 'your.model'


    @api.model

    def web_search_read(self, domain, fields, offset=0, limit=None, order=None, count_limit=None):

        # Domain contains the active filters in the list view

        print("Active domain (filters):", domain)

        # Fetch records visible in the list view

        records = self.search_read(domain, fields, offset=offset, limit=limit, order=order)

        print("Visible records:", records)

        # Proceed with the original behavior

        return super().web_search_read(domain, fields, offset=offset, limit=limit, order=order, count_limit=count_limit)


Hope it helps

0
Avatar
Discard
Avatar
Odiware Technologies
Best Answer

Hey there!

I totally get what you're trying to do - this is actually a pretty common challenge in Odoo development. You want to grab either the current domain filters or the list of record IDs that are showing in your list view from within a custom search function. The tricky part is that when you're inside a search method, you're kind of "in the middle" of the search process, so the usual active_ids approach doesn't work here.


If you can control when your search function gets called, you can pass the domain through the context:

python

# In your custom search method
def _search_your_field(self, operator, value):
    context = self.env.context
    current_domain = context.get('search_default_domain', [])
    
    # Now you can work with the current domain
    # and call your function with the relevant IDs
    if current_domain:
        filtered_records = self.search(current_domain)
        your_custom_function(filtered_records.ids)
    
    # Return your search domain
    return [('your_field', operator, value)]

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
Filters with multiple conditions
filter python deliveryorder
Avatar
0
Nov 24
1872
How i can apply filter into record ?
filter python orm
Avatar
Avatar
1
Sep 19
2887
[✅ SOLVED] How to make sure odoo.addons.base.models.ir_cron will log the number of processed records on a cronjob completion ?
python cronjob entreprise v18
Avatar
0
Jul 25
1487
Problem with date filter BETWEEN two dates
filter date between v18
Avatar
Avatar
Avatar
2
May 25
1894
[✅ SOLVED] How to extend a XML search filter view and add an additional filter ? Solved
filter searchview xmlview v18
Avatar
Avatar
1
May 25
3386
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