向导上的完整性错误 - Odoo v8

时间:2017-11-20 01:18:29

标签: python openerp odoo-8

关注我之前的question

我已设法将其余字段添加到向导中。

但是,我仍然面临一些问题,这是我目前的代码:

class generate_stock_picking(models.TransientModel):
    _name = 'generate.stock.picking'

    isbns = fields.One2many('order.picking', 'order_id', 'ISBN')
    print_order = fields.Many2one('bsi.print.order', 'Print Order')
    company_id = fields.Many2one('res.company', 'Company')
    location_id = fields.Many2one('stock.location', string="Source Location")
    location_dest_id = fields.Many2one('stock.location', string="Destination Location")

@api.model
def default_get(self, fields):
    res = super(generate_stock_picking, self).default_get(fields)
    isbns = []
    if self.env.context.has_key('active_id'):
        print_order = self.env.context['active_id']
        order = self.env['bsi.print.order'].browse(print_order)
        sp_types = self.env['stock.picking.type'].search([
        ('code', '=', 'incoming')
        ])
        if len(sp_types) > 0:
            for line in order.order_picking:
                if line.remaining_qty > 0:
                    val = {
                        #'origin': line.name,
                        'name': 'name',
                        'isbn': line.isbn.id,
                        'qty': line.remaining_qty,
                        'picking_type_id': sp_types[0].id,
                        'date_expected': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
                        }
                    isbns.append([0,0,val])
            res.update({'isbns':isbns,'print_order':print_order})
        return res

@api.multi    
def generate(self):
    print_order = self.env.context['active_id']
    order = self.env['bsi.print.order'].browse(print_order)
    for record in order:
        if self.isbns:
            order_picking = []
            sp_types = self.env['stock.picking.type'].search([
            ('code', '=', 'incoming')
            ])
            print_order = self.env['stock.picking'].create({
                'origin': record.name,
                'state': 'draft',
                'stock_picking_lines': self.print_order.id,
                'picking_type_id': sp_types[0].id,
                })
            for line in self.isbns:
                order_picking.append(self.env['bsi.print.order.lines'].create({
                    'print_order':print_order.id,
                    'isbn':line.isbn.id,
                    'qty':line.qty}))
                prod_isbn = self.env['bsi.print.order.lines'].search([('print_order','=',self.print_order.id),
                                                           ('isbn','=',line.isbn.id)])
                prod_isbn.consumed_qty = line.qty
            print_order.write({'order_picking':[(6,0,map(lambda x:x.id,order_picking))]})
            tree_view_id = self.env.ref('stock.vpicktree').id
            form_view_id = self.env.ref('stock.view_picking_form').id
            self.print_order.state = 'delivered'
            return {
                'name': _('Stock Picking'),
                'type': 'ir.actions.act_window',
                'res_model': 'stock.picking',
                'view_mode': 'tree,form',
                'view_type': 'form',
                'views': [(tree_view_id, 'tree'),(form_view_id, 'form')],
                'view_id':tree_view_id,
                'res_id': [print_order.id],
                'domain': [('id', 'in', [print_order.id])]
                 }


class order_picking(models.TransientModel):
    _name = 'order.picking'

    order_id = fields.Many2one('generate.stock.picking', 'Order')
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
    qty = fields.Float(string="Quantity")

当我点击向导时,我从我的模型中调用这个:

@api.multi
def open_wizard(self):
    return {
        'context': self.env.context,        
        'name': 'Move Product',
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': 'generate.stock.picking',
        'type': 'ir.actions.act_window',
    }

它抛出了我:

       Integrity Error

The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set

[object with reference: bsi.print.order - bsi.print.order] 

对象bsi.print.order是我尝试创建stock.picking的类。

有什么想法吗?

如果您需要进一步说明,请告诉我。

0 个答案:

没有答案