Odoo get day name based on a given date

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Odoo get day name based on a given date



i am newbie to odoo. I want to get the day name based on the date selected from the fields. I have the code as below :


from datetime import datetime, date
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError
import calendar

class HrPublicHolidayHolidays(models.Model):
_name = 'hr.public.holiday.holidays'
_description = 'Public Holidays Dates'

name = fields.Char('Holiday Name', required=True)
date = fields.Date('Holiday Date', required=True)
date_day = fields.Char('Day')
year_id = fields.Many2one('hr.public.holiday', 'Calendar Year', required=True)
variable = fields.Boolean('Date may change')

@api.onchange('date')
def _get_day_of_date(self):
for r in self:
selected = fields.Datetime.from_string(r.date)
r.date_day = calendar.day_name[selected.weekday()]



Then when i run the code, when i click on the add an item enter image description here



After i clicked the button it shows me the error result: enter image description here



But if i trying to select the date, it will shows the day of the date that i've selected like: enter image description here



So basically the code will run, but the error just keep on showing and i have no idea why.



If someone know what is the problem can you please help me?



Thank in advance.





You need to add condition of not false for day before calling weekday().
– khelili miliana
Jul 26 at 8:14






Just under 18th line, add : if r.date :
– khelili miliana
Jul 26 at 8:15



if r.date :





Hi Khelili, thank you very much. Such a stupid mistake i've made.
– Kowa JiaLiang
Jul 26 at 9:15





Hi Kowa, it is concentration mistake bro.
– khelili miliana
Jul 26 at 10:54





Add it as answer :-)
– CZoellner
Jul 26 at 14:57




1 Answer
1



first get the current date by default
ex: date = fields.Date(string='Date', default=fields.Date.context_today, required=True)



after that write this bellow function to get the name of the day,



ex:@api.multi
@api.depends('date')
def _get_day(self):
self.day = self.date and (datetime.strptime(self.date, '%Y-%m-%d').date()).strftime('%A') or ''






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered