Odoo Help


Many2one relation not saving properly

Charles Gervais-Dumont
on 3/9/15, 12:59 PM 535 views

I've been working on a MRP system to handle parts and BOM for electronic manufacturing.

Each bomline is related to a part, but when I save the bomline in a bom, all the bomlines point to part #1. If I edit and save a bomline, the line gets deleted.

Here are the related classes:


class bom(models.Model):

    _name = 'components.bom'
    _rec_name = 'resultingpart'

    lines = fields.One2many('components.bomline', 'partnumber', 'Items', ondelete='cascade')
    partcount = fields.Integer(compute='_compute_part_count', string="Part Count")
    resultingpart = fields.Many2one('components.part', 'Resulting Part', ondelete='no action')
    description = fields.Text()

    def _compute_part_count(self):
        self.partcount = 0



class bomline(models.Model):

    _name = 'components.bomline'

    name = fields.Char(string="Part Identifier", required=True)
    partnumber = fields.Many2one('components.part', string="Part Number", relation='line2part')
    description = fields.Text(string="Description", related='partnumber.description', readonly=True)



class part(models.Model):
    _name = 'components.part'
    _rec_name = 'part_number'

    part_id = fields.Integer(string="ID", required=True)
    part_number = fields.Char(compute='_compute_part_name', string="Part Number")
    description = fields.Text()
    part_type = fields.Many2one('components.part_type', 'Type', ondelete='no action')
    manufacturer_part = fields.Many2many('components.manufacturer_part', string="Manufacturer Part Number")

    def _compute_part_name(self):
        self.part_number = "GSS-P" + str(self.part_id).zfill(6)

    def getNext(self, cr, uid):
        recs = self.pool.get("components.part").search(cr, uid, [])
        if recs:
            next = 0
            for rec in recs:
                part_rec = self.browse(cr, uid, rec)
                next = max(next, part_rec.part_id)
            return next + 1
            return 1

    _defaults = {'part_id': lambda self, cr, uid, context: self.getNext(cr, uid),}




I think you were given wrong defination of the following field into bomline.

partnumber = fields.Many2one('components.part', string="Part Number", relation='line2part')

You have to remove relation from this field as like below.

partnumber = fields.Many2one('components.part', string="Part Number")

I hope it will resolve your issue.

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 3/9/15, 12:59 PM
Seen: 535 times
Last updated: 3/16/15, 8:10 AM