Community mailing list archives

community@mail.odoo.com

Re: many2many gives error "Expected Singleton"

by
Serpent Consulting Services Pvt. Ltd., Jay Vora
- 06/15/2015 09:55:45
For functional fields, usig @api.one is best!

On Mon, Jun 15, 2015 at 6:11 PM, Mathias Neef <mn@copado.de> wrote:

Problem solved. I forgotten to go deeper in rec.

    @api.depends('cp_childs_ids')
    def _childs_count_g16(self):
        count = 0
        if self.cp_childs_ids:
            for rec in self:
                for child in rec.cp_childs_ids:
                    age = self._birthday(child.birthday)
                    if age >= 16:
                        count += 1
   

Am 13.06.2015 um 23:20 schrieb Mathias Neef:
<blockquote cite="mid:557C9EAF.2000506@copado.de" type="cite"> Hi!

I have an error on a many2many field which is produced by ensure_one().

I try to extend the hr.employee with children name and birthday and some computed fields like number of childs, childs under 16 and childs 16+.
Hope someone can help me. Here is the complete code:

from datetime import datetime, date

from openerp import models, fields, api
from openerp.tools.translate import _


class hr_employee(models.Model):
    _inherit = 'hr.employee'
   
    cp_childs_ids = fields.Many2many('cp.hr.employee.childs', 'cp_hr_child_rel', 'employee_id', 'child_id',  string = 'Childs')
    cp_childs_count = fields.Integer(compute = '_childs_count', string = 'Number of Childs', readonly = True, store = True)
    cp_childs_count_s16 = fields.Integer(compute = '_childs_count_s16', string = 'Number of Childs under 16', readonly = True, store = True)
    cp_childs_count_g16 = fields.Integer(compute = '_childs_count_g16', string = 'Number of Childs 16 and greater', readonly = True, store = True)
   
    @api.multi
    @api.depends('cp_childs_ids')
    def _childs_count(self):
        if self.cp_childs_ids:
            list = [child for child in self.cp_childs_ids]
            self.cp_childs_count = len(list)
   
    @api.multi
    @api.depends('cp_childs_ids')
    def _childs_count_s16(self):
        count = 0
        if self.cp_childs_ids:
            for rec in self:
                age = self._birthday(self.cp_childs_ids.birthday)
                if age < 16:
                    count += 1
        self.cp_childs_count_s16 = count
   
    @api.multi   
    @api.depends('cp_childs_ids')
    def _childs_count_g16(self):
        count = 0
        if self.cp_childs_ids:
            for rec in self:
                age = self._birthday(self.cp_childs_ids.birthday)
                if age >= 16:
                    count += 1
        self.cp_childs_count_g16 = count       

    def _birthday(self, born):
        birth = datetime.strptime(born, '%Y-%m-%d')
        today = datetime.today()
        try:
            birthday = birth.replace(year = today.year)
        except ValueError:
            birthday = birth.replace(year = today.year, month = birth.month + 1, day = 1)
        if birthday > today:
            age = today.year - birth.year - 1
        else:
            age = today.year - birth.year             
        return age   
   
hr_employee()



--
Mit herzlichen Grüßen

Mathias Neef
Geschäftsführer

copadoMEDIA UG
Unterdorfstr. 29
77948 Friesenheim

Telefon: +49 (0)7821-3299-892
Fax: +49 (0)7821-3299-890

Email: info@copado.de
Web: www.copado.de | www.odoohosting.de

UST-ID: DE292335580
Registernummer: HRB 710679 - AG Freiburg

_______________________________________________
Mailing-List: https://www.odoo.com/groups/community-59
Post to: mailto:community@mail.odoo.com
Unsubscribe: https://www.odoo.com/groups?unsubscribe




--
The Splendid journey of 2 years in Numbers
---
Regards,
Jay Vora,
Managing Director,
<img src="cid:part1.09000700.05080805@serpentcs.com" style="color:rgb(136,136,136);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
Serpent Consulting Services Pvt. Ltd.
Web : http://www.serpentcs.com
Mobile : 91 - 9879354457
Twitter: jaynvora
Skype: jaynvora
Blog : http://jayvora.blogspot.com