3

my field

SCHEDULE_SELECTION = [
    ("no_schedule", "Без терміну"),
    ("overdue", "Протерміновано"),
    ("today", "Сьогодні"),
    ("this_week", "Цього тижня"),
    ("next_week", "Наступного тижня"),
    ("after_2_week", "Більше 2 тижнів"),
]
schedule_selection = fields.Selection(selection=SCHEDULE_SELECTION, string="Групування по терміну", compute='_compute_schedule_date')

def _compute_schedule_date(self):
    for i in self:
        
        ...

        if not i.schedule_date or i.done:
            i.schedule_selection = "no_schedule"
        elif i.is_overdue:
            i.schedule_selection = "overdue"
        else:
            selection_difference = (i.schedule_date - now).days
            if i.schedule_date.date() == now_date:
                i.schedule_selection = "today"
            elif selection_difference <= 7:
                i.schedule_selection = "this_week"
            elif selection_difference <= 14:
                i.schedule_selection = "next_week"
            else:
                i.schedule_selection = "after_2_week"   

and search:

<!-- this don`t work, i receive an error -->
<filter string='Термін' name='schedule_selection' domain="[]" context="{'group_by': 'schedule_selection'}"/>

as you can see, in this situation i can`t use "store=True", because this field depends by other fields(stored and computed) and current datetime.

on the Internet I found several posts where people say that for this you need to override the search() method, but I did not find specific examples.

Wald Sin
  • 158
  • 11

1 Answers1

2

For that you need the field to be stored in the database.
schedule_selection = fields.Selection(selection=SCHEDULE_SELECTION, string="Групування по терміну", compute='_compute_schedule_date', store=True)

Now Odoo can use the database grouping function. But Odoo does not know when to recalculate the schedule_selection field.
For this usually, there is @api.depends()

@api.depends("done", "schedule_date")
def _compute_schedule_date(self):
    for i in self:

This makes Odoo recalculate the field when done or schedule_date changes.

But you are also using now and that means you have to recalculate all the records every day. For that, you should make daily Scheduled Action that calls _compute_schedule_date on all the records that have the done field False.

Paxmees
  • 1,540
  • 1
  • 15
  • 28
  • i thought about it, it makes sense. thanks for the answer. This will mostly solve my problem, but I'll leave the question open for now. I want to determine for research purposes whether it is possible to do this and whether it is possible to do it effectively. – Wald Sin Jun 21 '23 at 06:37
  • 1
    Basically, if you want to overwrite the `search` function it would be too slow to do. You need to see all the lines to group them and then usually return only 80 lines. – Paxmees Jun 21 '23 at 07:09
  • 1
    The computed field also depends on the `is_overdue` field – Kenly Jun 21 '23 at 09:50