This question has been flagged
2 Replies
8815 Views

Hi guys i have some code like this

On the .py file

...

'date_received' : fields.date("Received Date")

'date_doc' : fields.date("Invoice Date")

...


On the .xml file i want to create search view on list view with filter domain date_received > date_doc, how can i achieve this?

Code below generate error 'undefined'

...

<filter domain="[('date_received','>', date_doc)]" help="Received Invoice"/>

...


Thanks

Avatar
Discard
Best Answer

Hello nuckles

I do have another solution to solve your problem. Let's create another boolean field to compare date range and use it on filter instead of using both date fields. You can follow the below things in the v7 coding pattern. 

According to v7 pattern

'is_received_date': fields.function(is_received_date, fnct_search=_func_search_is_received_date, method=True, type='boolean', string='Is Received Late')
def _func_search_is_received_date(self, cr, uid, obj, name, args, context):
match_ids = []
query = 'SELECT id FROM account_invoice ' \
'WHERE date_received > date_doc'
cr.execute(query)
for row in cr.fetchall():
match_ids.append(row[0])
if match_ids:
return [('id', 'in', match_ids)]
else:
return [('id', '=', 0)]


def is_received_date(self, cr, uid, ids, fieldnames, args, context=None):
res = {}
for line in self.browse(cr, uid, ids, context=context):
res[line.id] = line.date_received > line.date_doc
return res



According to Odoo v11 and v12 pattern

​compare_date = fields.Boolean(compute="_compute_compare_date", store=True)

@api.depends('date_received', 'date_doc')
def _compute_compare_date(self):
    for record in self:
        if record.date_received and record.date_doc and record.date_received > record.date_doc:
            record.compare_date = True
        else:
            record.compare_date = False

Add the following in your XML file.

<filter domain="[('compare_date','=', True)]" help="Received Invoice"/>

Hope this will help you. Please vote if you find a solution.

Thanks

Avatar
Discard
Author

Thanks for the answer, but if i take this approach then the existing record won't be filtered because it only affecting active form. I have to export all of the existing record and then update the compare_date my self so that the filter can work.

Whenever we are adding any new compute field in Odoo model. It will automatically compute for existing records.

Author

Yes, but the old record won't have the value

I meant that Odoo is already doing for old records as well.

Author

I'm using Openerp 7, so i can't use @api.depends. Any other way?

Add a new field with v7 syntax and update the existing record's value by preparing the Postgres query. It might be super easy to update the value of existing record

Author

I still can't get it to update every record. The active is updated fine. Here's my code

the field:

'is_received_date' : fields.function(is_received_date, method=True, type='boolean', string='Is Received Late', store=True)

the method:

def is_received_date(self, cr, uid, ids, fieldnames, args, context=None):

res = {}

for line in self.browse(cr, uid, ids, context=context):

if line.date_received > line.date_doc:

res[line.id] = bool(True)

else:

res[line.id] = bool(False)

return res

Author

I shorten the method to:

def is_received_date(self, cr, uid, ids, fieldnames, args, context=None):

res = {}

for line in self.browse(cr, uid, ids, context=context):

res[line.id] = line.date_received > line.date_doc

return res

Sorry, I didn't have v7 environment to test it but at least you have the idea now might be you are doing something wrong in code. Thanks

Author

i got it running, we need another function that search the whole record and place it in the field. Here's the code

the field:

'is_received_date' : fields.function(is_received_date, fnct_search=_func_search_is_received_date, method=True, type='boolean', string='Is Received Late')

the method:

def _func_search_is_received_date(self, cr, uid, obj, name, args, context):

match_ids = []

query = 'SELECT id FROM account_invoice ' \

'WHERE date_received > date_doc'

cr.execute(query)

for row in cr.fetchall():

match_ids.append(row[0])

print match_ids, 'lalalala match ids'

if match_ids:

return [('id', 'in', match_ids)]

else:

return [('id', '=', 0)]

def is_received_date(self, cr, uid, ids, fieldnames, args, context=None):

res = {}

for line in self.browse(cr, uid, ids, context=context):

res[line.id] = line.date_received > line.date_doc

return res

Can you edit your answer to this? and i will mark your answer as a correct one.

Thanks, I did it.

Best Answer

Hi,

Try to follow the below link

Filters using xml

Avatar
Discard
Author

Yes, i already know how to use normal filtering. But there is no example in the default module that compares 2 field from the same record.