Odoo Help

0

Many2one relation not saving properly

By
Charles Gervais-Dumont
on 3/9/15, 12:59 PM 593 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:

Bom:

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()

    @api.one
    def _compute_part_count(self):
        self.partcount = 0

 

Bomline:

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)

 

Part:

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")

    @api.one
    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
        else:
            return 1

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

 

0

Hi,

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.

Register

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)

Stats

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