Skip to Content
Menu
This question has been flagged
3 Replies
6518 Views

from odoo import models, fields, api

from datetime import timedelta, timeimport datetime
class kw_send_wish(models.Model):    

_name = 'kw_send_wish'    

_description = 'A model to manage send wishes'
birth_date = fields.Boolean(compute='show_birthday')
        

@api.one    

def show_birthday(self): 

record = self.env['hr.employee'].sudo().search([])        

today = datetime.datetime.today()        

for birthday in record: 

 print(birthday) 

 birth_date = record.birthday            

if str(record.birth_date)==str(today): 

 record.birth_date = True
Above is my code and showing the error that :-

raise ValueError("Expected singleton: %s" % self)
ValueError: Expected singleton: hr.employee(26, 23, 10, 7, 6, 27, 9, 8, 18, 16, 15, 14, 25, 11, 17, 21, 19, 24, 20, 22)
Avatar
Discard

This code is absolutely difficult. Do in better logic. Why you are getting all employees always? and avoid loops ie, making system glitch. Search employees with birthdate today or within a date span. Use domain in search accordingly. Whats the need of a new model? just use the same employee kanban view by filtering employees.

Best Answer

Hi.

For solving the singleton error either you can iterate the self over the for loop or use self.ensure_one . Here you have already iterated over the for loop, update the code like this,


def show_birthday(self):
record = self.env['hr.employee'].sudo().search([])
today = datetime.datetime.today()
for birthday in record:
print(birthday)
if str(birthday.birth_date) == str(today):
birthday.birth_date = True

Thanks

Avatar
Discard

edit: don't do string comparisons for dates or DateTime. instead, do fields.Date.from_string(input) == today # today should be date object

Best Answer


Hi,   

Can you please explain the functionality that you want. If you want to send birthday wishes to employee just have a look at the Odoo Aps


Avatar
Discard
Author Best Answer

Thanks for replaying.

Actually my requirement is :- I have 2 model like("hr.employee" And "kw_send_wish"). I want to bring the employee name,phno,email fields from "hr.employee" whose birthday, year of service and anniversary is today and show it in  kanban view with separate column for each wishes and send wishes to them.

If any idea please share.

Avatar
Discard