This question has been flagged

 I have this method:

@api.multi
def create_print(self):
    rec_production_order = self.env['bsi.production.order'].browse(1)
    self.env['bsi.print.order'].create({
        'origin': rec_production_order.id,
        'state': 'draft',
    })

Which should create another model, from bsi.production.order, these are the two complete models:

class bsi_production_order(models.Model):
    _name = 'bsi.production.order'
    _inherit = ['product.product']

    @api.model
    def create(self, vals):
        if vals.get('name', 'New') == 'New':
            if vals.get('production_type') == 'budgeted':
                vals['name'] = self.env['ir.sequence'].next_by_code('bsi.production.budgeted') or '/'
            elif vals.get('production_type') == 'nonbudgeted':
                vals['name'] = self.env['ir.sequence'].next_by_code('bsi.production.non_budgeted') or '/'
            elif vals.get('production_type') == 'direct':
                vals['name'] = self.env['ir.sequence'].next_by_code('bsi.production.direct') or '/'
        return super(bsi_production_order, self).create(vals)

    name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
    date = fields.Date(string="Production Date")
    notes = fields.Text(string="Notes")
    order_lines = fields.One2many('bsi.production.order.lines', 'production_order', states={'finished': [('readonly', True)], 'cancel': [('readonly', True)]}, string="Order lines", copy=True)
    print_orders = fields.One2many('bsi.print.order', 'production_orders', string="Print Orders")

class bsi_print_order(models.Model):
    _name = 'bsi.print.order'
    _inherit = ['mail.thread','mrp.worksheet.contract'] 

    name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
    date = fields.Date(string="Print Date")
    production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
    origin = fields.Char(string="Origin")
    due_date = fields.Date(string="Due Date")
    state = fields.Selection([
        ('draft','Draft'),
        ('awaitingraw','Awaiting raw materials'),
        ('wip','Work in Progress'),
        ('delivered','Delivered'),
        ('cancel','Cancel'),
    ], string="State")
    notes = fields.Text(string="Notes")

The method works, but on origin field from bsi.print.order it just creates a 1, it is supposed to be the name from bsi.production.order, not a number, also, what is weird about this, is the fact that, no matter how many print orders I create, it always puts a number 1 in there.

Any ideas?

Avatar
Discard
Best Answer

Hello Alberto,

@api.multi

def create_print(self):
    rec_production_order = self.env['bsi.production.order'].browse(1)
    self.env['bsi.print.order'].create({
        'origin': rec_production_order.id,
        'state': 'draft',
    })

In your code, you put the id of the bsi_production_order.

Try this :-

@api.multi

def create_print(self):
    self.env['bsi.print.order'].create({
        'origin': self.name,
        'state': 'draft',
    })


Hope it will works for you.
Thanks,

Avatar
Discard
Author

Hi Jignesh, Thank You very much, but when I put name, then I try to create a production order, or even a print.order from the method it throws me ´MissingError

One of the documents you are trying to access has been deleted, please try again after refreshing.´ :/

Hello Alberto, id which is 1 you browse from production.order, it was deleted. so try to search with specific domain. Thanks,

Author

Thanks, How can I do that in this specific scenario?

What do you want when your create_print method fired ?

Author

To create a new bsi.print.order object, with the bsi.production.order 'name' as 'origin' field in bsi.print.order, and 'state' in 'draft', but the problem is 'name'. I've tried with .browse and .create(), with no success :(

create_print method in which object ?

Author

bsi.production.order :)

see my updated answer in bold. Thanks