跳至内容
菜单
此问题已终结
2 回复
10657 查看

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

形象
丢弃
最佳答案

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

形象
丢弃
编写者

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.

编写者

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

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

编写者

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

编写者

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

编写者

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

编写者

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.

最佳答案

Hi,

Try to follow the below link

Filters using xml

形象
丢弃
编写者

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.

相关帖文 回复 查看 活动
0
6月 18
3299
0
10月 15
4191
1
5月 24
2603
1
2月 24
5164
1
11月 22
4941