添加一个自定义按钮,而无需Close Odoo 9向导



美好的一天,

我一直在Odoo 9中的批次和序列号模块。

我更改了模块的默认顺序,然后用uuid的产生代替了它,但是当我在接收到的项目中调用此组件时,当我单击生成uuid的按钮时,应用程序突然返回了应用程序到我曾经打电话给它的窗口,而无需让我保存我生成的uuid。

这是我的代码:

class stock_production_lot(osv.osv):
_name = 'stock.production.lot'
_inherit = ['mail.thread']
_description = 'Lot/Serial'
_columns = {
    'name': fields.char('Serial Number', required=True, help="Unique Serial Number"),
    'x_num_serie_': fields.char('No. de serie', required=False, help="No. de serie del producto"),
    'ref': fields.char('Internal Reference', help="Internal reference number in case it differs from the manufacturer's serial number"),
    'product_id': fields.many2one('product.product', 'Product', required=True, domain=[('type', 'in', ['product', 'consu'])]),
    'quant_ids': fields.one2many('stock.quant', 'lot_id', 'Quants', readonly=True),
    'create_date': fields.datetime('Creation Date'),
}
_defaults = {
    'name': lambda x, y, z, c: x.pool.get('ir.sequence').next_by_code(y, z, 'stock.lot.serial'),
    'x_num_serie_':None,
    'product_id': lambda x, y, z, c: c.get('product_id', False),
}
_sql_constraints = [
    ('name_ref_uniq', 'unique (name, product_id)', 'The combination of serial number and product must be unique !'),
]        
def action_traceability(self, cr, uid, ids, context=None):
    """ It traces the information of lots
    @param self: The object pointer.
    @param cr: A database cursor
    @param uid: ID of the user currently logged in
    @param ids: List of IDs selected
    @param context: A standard dictionary
    @return: A dictionary of values
    """
    quant_obj = self.pool.get("stock.quant")
    quants = quant_obj.search(cr, uid, [('lot_id', 'in', ids)], context=context)
    moves = set()
    for quant in quant_obj.browse(cr, uid, quants, context=context):
        moves |= {move.id for move in quant.history_ids}
    if moves:
        return {
            'domain': "[('id','in',[" + ','.join(map(str, list(moves))) + "])]",
            'name': _('Traceability'),
            'view_mode': 'tree,form',
            'view_type': 'form',
            'context': {'tree_view_ref': 'stock.view_move_tree'},
            'res_model': 'stock.move',
            'type': 'ir.actions.act_window',
                }
    return False

def action_generate_uuid(self, cr, uid, ids, context=None):
    print "< action_generate_uuid >"
    _uuid = (uuid.uuid1()).hex
    obj = self.browse(cr, uid, ids,context=context)
    print "< obj.name >",obj.name
    for item in self.browse(cr, uid, ids,context=context):
        if item.name:                
            item.name = _uuid 
            item.x_num_serie_ = _uuid
            print "< name >",item.name
            print "< x_num_serie_>",item.x_num_serie_
        else:
            print "< falta un elemento >"
    return None

我将非常感谢关于发生的事情以及如何避免发生的任何想法。

最好的问候

alain

默认行为是在按下任何按钮的按钮和与按钮关联的函数执行时关闭。周围的工作是让按钮执行功能,然后返回一个动作,以提出完全相同的向导。

您可以将上下文设置为再次打开向导,并使用填充的所有表单值。

这是一个示例:

class MyWizard(models.TransientModel):
    _name = 'myaddon.mywizard'
    def _get_default_char(self):
        return self._context.get('mychar',"")
    mychar = fields.Char(string="My Char", default=_get_default_char)
    @api.multi
    def my_button(self):
        # Execute Function Here
        # reload wizard with context
        return {
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'myaddon.mywizard',
            'type': 'ir.actions.act_window',
            'target': 'new',
            'res_id': self.id,
            'context': '{"default_mychar":'HELLO WORLD'}',
        }

最新更新