Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
7254 Lượt xem

Dear all,
I am having problems on comparing 2 dates in Odoo 12.
I have a variable that contains a date. If I print it I get:

>> print(date)
2019-05-11

I also have a field of date type with a calender widget set on xml.

So, when I print it's value I get:

>> print vals.get('date')
2019-05-11

The problem is that I am unable to compare both fields. I get no error and the "if statement" is not read.

if date == vals.get('date'):
    print("Date is equal")

The full code is:

registos = self.env['mymodule.mymodel'].search([('myfield.id','=',value_to_search)])
for rec in registos:
    date = rec.date
    if date == vals.get('date'):
        print("Date is equal")

What's wrong here?

Thank you in advance

Best regards

Paulo



Ảnh đại diện
Huỷ bỏ

Can you just check whether both the fields/value or of same data type...

Tác giả

Thank you @Niyas.

This was exactly the problem. Both values had different type values.

vals.get('date') #string

rec.date #date

I have solved the issue.

Thank you once again

Câu trả lời hay nhất

Here you are comparing two strings where odoo retrieves date or DateTime fields as a string. So here you are comparing str == str and results vary according to that. The solution is that you have to typecast the fields before comparison using pythons default or odoo's way. I can give you an example.

date = vals.get('date') # String
date = feilds.Date.from_string(date) # Date(01,02,2019) dateobject

Output:-
Ảnh đại diện
Huỷ bỏ
Tác giả

Thank you @Hilar,

I solved the problem by converting "rec.date" to string in order to be able to compare both values "as strings", since "vals.get('date')" is already a string. It solved my problem.

Thank you once again

No, Thats wrong. you have to convert it to the date object for comparison. else the result will be wrong.

Tác giả

I have already tried @Hilar and I get correct results.

Based on your last comment:

vals.get('date') #string

date.today() #date object

data = vals.get('date') #string

data_hoje = str(date.today()) #string

Using, "if data > data_hoje:" (as an example), gives me exactly the results I expect...

Please check my updated answer and look the last example.

You should understand the logic that the string > string doesn't gonna give you the exact result for comparing two dates, that is Date(10,01,2018) > Date(11,01,2019). See this example
image.png



On Wed, May 15, 2019 at 9:57 PM Paulo Matos <batalhadematos@gmail.com> wrote:

I have already tried @Hilar and I get correct results.

Based on your last comment:

vals.get('date') #string

date.today() #date object

data = vals.get('date') #string

data_hoje = str(date.today()) #string

Using, "if data > data_hoje:" (as an example), gives me exactly the results I expect...

Sent by Odoo S.A. using Odoo.



--
Hilar AK  Python / Odoo Developer

m: +917736617619
e: hilarak@gmail.com
Follow me:
twitteryoutubegithubstack-overflow


Tác giả

Thank you @Hilar AK, now I fully understood the logic. You are right. I will try to typecast the fields so I get the desired "safe" results. Thank you once again. Best regards

Câu trả lời hay nhất

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 6 25
1206
1
thg 4 25
3662
1
thg 1 25
18033
0
thg 3 24
1396
0
thg 10 22
2581