Skip to Content
Menu
This question has been flagged
1 Reply
9049 Views

I'm writing a module to expand on the sales report. I need to add filters for specific time periods relative to the current date. I can use either python or postgres to calculate the needed date range, but I'm having a problem using those dates in the xml for the view. Primarily I'm trying to use a field or a python function as the value for a domain in the search filter. Is this possible?

When trying to use a field, I generally get an evaluation error like: 

NameError: name 'end_last_week' is not defined

This happens both when the field is defined by a compute function and when it's defined by the database query and I can confirm that the field is populated in the model table. The relevant search filter element I'm trying to use looks like this:

<filter name="Last Week" string="Last Week" domain="[('state','not in',('draft', 'cancel', 'sent')), ('date','<=',end_last_week),('date', '>=',start_last_week)]"/>

How can I use a computed variable as the value for a domain?  Any insight is appreciated.

EDIT:

For reference, here is an example definition for a field I want to use as a domain value:

    def _compute_end_last(self):
last_week_end = str((datetime.now().date()) - timedelta(days=(datetime.now().date()).weekday() + 3))
return last_week_end

end_last_week = fields.Char('End Last', compute='_compute_end_last', store=True)


Avatar
Discard
Best Answer

Yes, you can use a computed variable as a value for a domain, the thing is that the computed field has to be store=True or if it's store=False (Odoo's default if no store value specified) you need to specify a search='_search_method()' attribute to the field.

ie.

upper_name = field.Char(compute='_compute_upper', search='_search_upper')

def _search_upper(self, operator, value):
    if operator == 'like':
        operator = 'ilike'
    return [('name', operator, value)]

you can take a look at the documentation of computed fields in Odoo right here: https://www.odoo.com/documentation/13.0/reference/orm.html#computed-fields

Avatar
Discard
Author

Thanks for your reply. I have tried setting store=True, but I still get the 'is not defined' error and the field is not populated in the database table. I have added an example field definition I've tried to use. Does it look right?

your compute method is wrong, it has to be like this:

def _compute_end_last(self):

self.last_week_end = str((datetime.now().date()) - timedelta(days=(datetime.now().date()).weekday() + 3))

end_last_week = fields.Char('End Last', compute='_compute_end_last', store=True)

Author

I tried making that change, but I'm still getting the same error.

Related Posts Replies Views Activity
1
Oct 22
1351
0
Jan 18
3808
1
May 15
4585
0
May 23
1395
1
Mar 15
6469