Skip to Content
Menu
This question has been flagged
1 Reply
10099 Views

I am inheriting the create function for the mrp.production model and I am trying to create a mrp.partsorder record that is related to the first. My function is creating records in both tables in the database, but the "sourceorder_id" field is empty. I've tried several variations, but this is my current code:

 class mrp_production(osv.osv):
    """
    Production Orders / Manufacturing Orders
    """
    
    _inherit = 'mrp.production'
    
    _columns = {
        'partsorder_ids': fields.one2many('mrp.partsorder', 'sourceorder_id', string="Parts Orders"),
}
    
    def create(self, cr, uid, vals, context=None):
        res = super(mrp_production, self).create(cr, uid, vals, context)
        partsorder_ids = self.pool.get('mrp.partsorder').create(cr,uid, vals, context=context)
        return res

class mrp_partsorder(osv.Model): _name = 'mrp.partsorder' _columns = {         'completedby_id': fields.many2one('res.users', string="Completed By"), 'sourceorder_id': fields.many2one('mrp.production', string="Source Order") }


I've also tried to assign a tuple like so:

partsorder_id = self.pool.get('mrp.partsorder').write(cr, uid, [partsorder], {'partsorder': [(0,0, {'completedby_id'=user.id'})]}) 

I've even tried just assigning a tuple to the one2many variable so that it will be called and assigned using (0,0). Like so

 def create(self, cr, uid, vals, context=None):
        res = super(mrp_production, self).create(cr, uid, vals, context)
        partsorder_ids = []
        partsorder_ids.append((0,0, {'scheduleDate':'meowmix9'}))
        return res

My goal is to simply have a record created in the marp.partsorder model that is related to the mrp.production model everytime an mrp.production record is created. The create function works fine, but no data is stored in the database for the relationship. In other words the sourceorder_id field is empty on the partsorder table in the database. 

Avatar
Discard
Best Answer


class mrp_production(osv.osv):
    """
    Production Orders / Manufacturing Orders
    """
    
    _inherit = 'mrp.production'
    
    _columns = {
        'partsorder_ids': fields.one2many('mrp.partsorder', 'sourceorder_id', string="Parts Orders"),
}
    
    def create(self, cr, uid, vals, context=None):
context = context or {} created_id = super(mrp_production, self).create(cr, uid, vals, context)
values = (vals.get('partsorder_ids', [])) + [(0, 0, {'sourceorder_id': created_id,
completedby_id': uid})]
self.write(cr, uid, [created_id], {'partsorder_ids': values}, context=context)
return created_id

class mrp_partsorder(osv.Model):
_name = 'mrp.partsorder'
_columns = {
'completedby_id': fields.many2one('res.users', string="Completed By"),
'sourceorder_id': fields.many2one('mrp.production', string="Source Order")
}



Avatar
Discard
Author

Firstly, this solves my problem. A related record is now created. (Thank you!) That said, I have a follow up question. I am confused about the logic of this function and when the new record is actually created in mrp.production. The super().create is assigned to created_id. Then, it's called when you run self.write and "values" has to run create_id in order to return the id for the sourceorder_id dictionary key. Then, you return the created_id function which, in my mind, would then call the super().create function. Would anyone mind giving me a quick breakdown on the logic of this function. I think it would definitely help my understand of odoo functionality and python.

when you do a call function with super in create function (created_id = super(mrp_production, self).create(cr, uid, vals, context)), you have the new record created, and the super return id of the record created, now the record exists, you can use the id now it exists, you can use write function too to change its values.

Related Posts Replies Views Activity
1
Mar 23
787
0
Dec 22
1516
0
Jun 21
1843
0
Jun 20
4179
1
Nov 19
1743