Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
1 Vastaa
7492 Näkymät

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
Hylkää
Paras vastaus

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
Hylkää
Tekijä

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

Tekijä

@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

Tekijä

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

Tekijä

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

Tekijä

@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

Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
2
tammik. 24
1780
2
kesäk. 22
3599
2
kesäk. 18
5433
2
marrask. 16
12572
2
helmik. 24
11706