Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
474 Vistas

Hello,


I have created a custom date field "birthdate".


When I try to apply the “BETWEEN” operation to two dates by creating a custom filter, the system calls the search function twice, once with the >= operation and once with the <= operation.


In the results, Odoo shows me all the records responding to the first query AND all the records responding to the second. 


How can I get Odoo to display only the results that match both queries?


Thanks in advance



Avatar
Descartar
Autor

Thank you for your reply.

Is it possible to allow the user to choose the date range?

Mejor respuesta

If you're building this filter in the XML of the search view or using the search domain directly in code (e.g., Python or JS), you must explicitly structure the domain with an AND condition like:
[('birthdate', '>=', start_date), ('birthdate', '<=', end_date)]


Avatar
Descartar
Autor

Thanks for your reply,
That's what I'm thinking, but how can I allow the user to choose the start and end dates ?

Mejor respuesta

Hi Mr Tessa
check this 

<!-- Define a custom search view with proper date range filters -->
<record id="view_your_model_search" model="ir.ui.view">
    <field name="name">your.model.search</field>
    <field name="model">your.model</field>
    <field name="arch" type="xml">
        <search string="Search Records">
            <field name="name"/>
            <field name="birthdate"/>
            
            <!-- Custom date range filter -->
            <filter name="birthdate_range" string="Birth Date Range" 
                    domain="[]" 
                    context="{'search_default_birthdate_range': 1}"/>
            
            <!-- Predefined date ranges -->
            <separator/>
            <filter name="born_this_year" string="Born This Year"
                    domain="[('birthdate', '>=', context_today().strftime('%Y-01-01')),
                             ('birthdate', '<=', context_today().strftime('%Y-12-31'))]"/>
            
            <filter name="born_last_year" string="Born Last Year"
                    domain="[('birthdate', '>=', (context_today() - relativedelta(years=1)).strftime('%Y-01-01')),
                             ('birthdate', '<=', (context_today() - relativedelta(years=1)).strftime('%Y-12-31'))]"/>
            
            <group expand="0" string="Group By">
                <filter string="Birth Month" name="group_birth_month"
                        context="{'group_by': 'birthdate:month'}"/>
                <filter string="Birth Year" name="group_birth_year"
                        context="{'group_by': 'birthdate:year'}"/>
            </group>
        </search>
    </field>
</record>
Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
jun 21
5123
1
sept 16
56
0
mar 15
4644
3
may 25
566
1
feb 23
5734