【问题标题】:Store data on button click from odoo website从 odoo 网站单击按钮存储数据
【发布时间】:2015-11-09 19:45:45
【问题描述】:

我想将来自 odoo 网页的数据存储到 odoo 数据库中。我从 odoo 网站构建器创建了一个新页面。它有几个输入字段和一个提交按钮。我想通过单击提交按钮将该字段的数据存储到数据库中的表中。 Odoo 文档只讲述了如何从数据库中读取数据到网页,而不是如何将数据从网页存储到数据库。有人知道怎么做吗?

下面是我的代码:

控制器:

@http.route('/tasks/clocktime', type='http', auth='user', website=True)
    def clock_time(self, **post):
        task_pool = request.registry['project.task']
        task_pool.attendance_action_change()
        return

模板:

<form target="_self" action="/tasks/clocktime" method="post">
    <a class="btn btn-primary a-submit">Log In/Out</a>
</form>

【问题讨论】:

    标签: python database web openerp webpage


    【解决方案1】:

    点击提交按钮后,你必须路由到一个方法。在该方法中,您可以使用普通的 odoo 函数(如(创建、写入))将数据存储到数据库中。
    你必须使用 request.registry['model.name'].method(......)
    where 方法是根据需求创建/写入
    我正在粘贴来自 website_sale 模块的示例代码,它将数据写入 sale_order 模型

    @http.route(['/shop/payment/transaction/<int:acquirer_id>'], type='json', auth="public", website=True)
        def payment_transaction(self, acquirer_id):
            cr, uid, context = request.cr, request.uid, request.context
            transaction_obj = request.registry.get('payment.transaction')
            order = request.website.sale_get_order(context=context)
    
            if not order or not order.order_line or acquirer_id is None:
                return request.redirect("/shop/checkout")
    
            assert order.partner_id.id != request.website.partner_id.id
    
            # find an already existing transaction
            tx = request.website.sale_get_transaction()
            if tx:
                if tx.state == 'draft':  # button cliked but no more info -> rewrite on tx or create a new one ?
                    tx.write({
                        'acquirer_id': acquirer_id,
                        'amount': order.amount_total,
                    })
                tx_id = tx.id
            else:
                tx_id = transaction_obj.create(cr, SUPERUSER_ID, {
                    'acquirer_id': acquirer_id,
                    'type': 'form',
                    'amount': order.amount_total,
                    'currency_id': order.pricelist_id.currency_id.id,
                    'partner_id': order.partner_id.id,
                    'partner_country_id': order.partner_id.country_id.id,
                    'reference': order.name,
                    'sale_order_id': order.id,
                }, context=context)
                request.session['sale_transaction_id'] = tx_id
    
            # update quotation
            request.registry['sale.order'].write(
                cr, SUPERUSER_ID, [order.id], {
                    'payment_acquirer_id': acquirer_id,
                    'payment_tx_id': request.session['sale_transaction_id']
                }, context=context)
    
            return tx_id
    

    【讨论】:

    • 感谢 Shravan。您还可以发布调用此方法的 xml 代码吗?
    • 您可以参考任何 odoo 网站* 模块视图文件夹。一般在form action属性中我们会给出这样的url,例如:
      ,odoo会寻找路由到该url的相应方法
    • 我无法从按钮调用函数。请参阅编辑中的代码。
    • 你遇到了什么问题。
    • 方法没有被调用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 2021-12-02
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多