Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
5429 Visualizzazioni

I wanna show only one of these three in Odoo but I seem to not do it. I have tried importing datetime module and doing datetime.now().year but Odoo isn't recognizing it. How do I solve this?


Many thanks!

Avatar
Abbandona
Risposta migliore

By context i think it is a function call problem. To import correctly the datetime module you should do this:


from datetime import datetime

current_year = datetime.now().year
print(current_year)
Avatar
Abbandona
Autore

But then it says "field current_year doesn't exist". I have another option where I could just strip each piece from Odoo's datetime field but to me looks performance expensive doing stripping where I may have easier ways.

Ok. Your issue is clearer now. Try this:

from datetime import datetime
from odoo import models, fields

class MyModel(models.Model):
_name = 'my.model'

current_year = fields.Integer(string='Current Year', default=lambda self: datetime.now().year)

Autore

Big UP Bro! It really worked! But I have this problem that year 2023 is with a comma as 2,023. I have searched and tried like "widget='integer' " and also added the format options but it didn't work too. How do I solve this?

You can edit the field to Char. Try this:

current_year = fields.Char(string='Current Year', default=lambda self: datetime.now().year)

Autore

Bless you my friend! It worked like a charm!

Post correlati Risposte Visualizzazioni Attività
1
apr 22
2749
3
nov 23
17519
3
nov 24
25160
1
apr 23
6205
2
dic 22
6959