Skip to Content
Menu
This question has been flagged
2 Replies
268 Views

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
Discard
Author

Thank you for your reply.

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

Best Answer

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
Discard
Author

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

Best Answer

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
Discard
Related Posts Replies Views Activity
1
Jun 21
4930
1
Sep 16
56
0
Mar 15
4443
3
May 25
389
1
Feb 23
5533