Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
311 Vizualizări

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



Imagine profil
Abandonează
Autor

Thank you for your reply.

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

Cel mai bun răspuns

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


Imagine profil
Abandonează
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 ?

Cel mai bun răspuns

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>
Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
1
iun. 21
4956
1
sept. 16
56
0
mar. 15
4524
3
mai 25
420
1
feb. 23
5624