Ir al contenido
Menú
Se marcó esta pregunta
3 Respuestas
832 Vistas

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

Avatar
Descartar

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

Autor Mejor respuesta

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

Avatar
Descartar
Mejor respuesta

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

Avatar
Descartar
Mejor respuesta

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

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
0
nov 24
1136
1
sept 19
2417
0
jul 25
355
2
may 25
750
1
may 25
1461