Se rendre au contenu
Menu
Cette question a été signalée
1 Répondre
7510 Vues

i have created a model with a Date and a Datetime field, want to know to set default values:

1) how i can add Day(s) / Week(s) / Month(s) in Date and Datetime field in model to assign a default value?

2) how to add time in Datetime field in model to assign a default value?

right now, i have following default values and if i want by default add 3 days in today's Date how i can ( same for week and month ) and what if i want to add 30min or 1 hour in time ( Datetime field )  

appointment_time = fields.Datetime (string='Appointment Time', default=fields.Datetime.now)
booking_date = fields.Date(string='Booking Date', default=fields.Date.context_today)


please help.

regards

Avatar
Ignorer
Meilleure réponse

Hi,

If you search inside the odoo source code for the usage of relativedelta you will get a lot of such samples.

lead_days_date = fields.Date.today() + relativedelta.relativedelta(days=10)

The above code will add 10 days to today's date, similar you can change month and year.


For more: https://dateutil.readthedocs.io/en/stable/relativedelta.html

Thanks

Avatar
Ignorer
Auteur

thank you @Niyas Raphy , will check the link to learn further. regards

Auteur

@Niyas Raphy, i just tried your code as below but when upgrading my app/module, it is saying that booking_date field is not in database ( i have used in patient_view.xml ).

booking_date = fields.Date.today() + relativedelta(days=3)

how i can have the field with data or this code is used in a compute function? or something else? i searched odoo default addons folder for relativedelta yet failed to find how to set default value as mentioned in my opening post above. or i am missing something or this is my wrong approach etc.?

please help.
regards

Auteur

when using relativedelta.relativedelta(days=3) it is not starting server and showing error

type object 'relativedelta' has no attribute 'relativedelta'

may be i have to import something for this purpose.
regards

Auteur

for field booking_date which type is Date is working fine now with code below:

booking_date = fields.Date(default=str(datetime.now() + relativedelta.relativedelta(days=3)))

but for Datetime type field it is thowing error when going to create New record. error as below (the last line only because there are several lines):

ValueError: unconverted data remains: .191905

may be i have to use some other pythong function to convert it correctly for Datetime field, please help.
regards

Auteur

@Niyas Raphy thank you for helping us.

its done now, also modified Datetime type field to have default date + time as below:

appointment_time = fields.Datetime(default=datetime.today() + relativedelta.relativedelta(days=3, hours=3))

please advise if i am doing wrong or missing something.
regards

Publications associées Réponses Vues Activité
2
janv. 24
1783
2
juin 22
3603
2
juin 18
5437
2
nov. 16
12575
2
févr. 24
11710