Se rendre au contenu
Menu
Cette question a été signalée
2 Réponses
343 Vues

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
Ignorer
Auteur

Thank you for your reply.

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

Meilleure réponse

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
Ignorer
Auteur

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

Meilleure réponse

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
Ignorer
Publications associées Réponses Vues Activité
1
juin 21
4996
1
sept. 16
56
0
mars 15
4560
3
mai 25
443
1
févr. 23
5648