콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
2 답글
11046 화면

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
3495
0
10월 15
4442
1
5월 24
2889
1
2월 24
5433
1
11월 22
5269