【问题标题】:Add a custom button without close odoo 9 wizard?添加自定义按钮而不关闭 odoo 9 向导?
【发布时间】:2017-08-05 02:20:40
【问题描述】:

早安,

我一直在 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

我真的很感激任何关于正在发生的事情以及如何避免它的想法。

最好的问候,

阿兰

【问题讨论】:

    标签: openerp wizard


    【解决方案1】:

    默认行为是在按下任何按钮并执行与该按钮相关的功能时关闭。解决方法是让按钮执行一个功能,然后返回一个动作,从而调出完全相同的向导。

    您可以设置上下文以再次打开向导并填充所有表单值。

    这是一个例子:

    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'}',
            }
    

    【讨论】:

    • 感谢分享知识!
    • 哦,我刚刚在帖子中注意到的一件事。在 Odoo 的表单视图中预填充字段时,您传递一个上下文对象,其语法如下 {"default_{{your_field_name}}":"value"} 请注意字段名称前面的“default”。我想我最初在帖子中错过了这个,后来它已经更新了。
    猜你喜欢
    • 2015-11-04
    • 1970-01-01
    • 2011-06-09
    • 2018-01-31
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多