Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
3 Antworten
7225 Ansichten

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



Avatar
Verwerfen

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

Autor

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

Beste Antwort

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:-
Avatar
Verwerfen
Autor

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.

Autor

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


Autor

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

Beste Antwort

Avatar
Verwerfen
Verknüpfte Beiträge Antworten Ansichten Aktivität
3
Juni 25
1179
1
Apr. 25
3642
1
Jan. 25
18024
0
März 24
1381
0
Okt. 22
2562