【问题标题】:How to redirect from a clicked list in tree view to certain form view in Odoo using Python?如何使用 Python 从树视图中的单击列表重定向到 Odoo 中的某些表单视图?
【发布时间】:2017-07-06 14:43:30
【问题描述】:

我正在使用 API 9。首先,我制作了一个按钮来显示客户选择的一些详细产品的树形视图(如弹出窗口),该按钮名为 View Chosen此图片中的产品 (see this link)。

然后当点击按钮时,它会显示所选产品的树形视图,例如in this link

所以,我想做的是,如果我们单击列表/树视图弹出窗口中的产品,我想创建一个重定向链接。因此它将重定向到我们在列表中单击的产品的表单视图。列表中的产品是客户选择购买的产品。我的按钮只显示客户在购物时选择的产品。

我应该在我的代码中添加什么或者我应该改变什么?我真的是 Python 和 Odoo 的新手。

这是我的按钮 Python 代码:

from openerp import models, api, fields, _ 

class task_04 (models.Model):

    #Inherit dari model purchase.order
    _inherit    =   "purchase.order"

    @api.multi
    def action_view_related_products(self):
        ids = [line.product_id.id 
               for line in self.order_line]
        return{
            'name'          :   ('View Chosen Products'), # Nama dari tabel pop up
            'type'          :   'ir.actions.act_window',
            'view_type'     :   'form', #Tampilan pada tabel pop-up
            'view_mode'     :   'tree', # Menampilkan bagian yang di pop up, tree = menampilkan tabel tree nya utk product 
            'res_model'     :   'product.product', #Menampilkan tabel yang akan di show di pop-up screen
            'target'        :   'new', # Untuk menjadikan tampilan prduct yang dipilih menjadi pop-up table tampilan baru, jika dikosongin maka tidak muncul pop-up namun muncul halaman baru.
            'view_id'       :   False,
            'domain'        :   [('id','in',ids)] #Filter id barang yang ditampilkan
            }

task_04()

这是我查看按钮的 XML 代码:

<openerp>
<data>
    <record id="task_4_purchase_order_form" model="ir.ui.view">
        <field name="name">purchase.order.form</field>
        <field name="model">purchase.order</field>
        <field name="inherit_id" ref="purchase.purchase_order_form"></field>
        <field name="arch" type="xml">
            <!-- Lokasi untuk menempatkan button yang akan dibuat diletakkan di sebelah button cancel -->
            <xpath expr="/form/header/button[@name='button_cancel']" position="inside">
                <!-- Membuat button  -->
                <button string="View Chosen Product(s)" type="object" name="action_view_related_products"/>
            </xpath>
        </field>
    </record>
</data>
</openerp>

【问题讨论】:

    标签: python xml button openerp odoo-9


    【解决方案1】:

    试试这个代码:-

      #Inherit dari model purchase.order
    _inherit    =   "purchase.order"
    
    
    @api.multi
    def action_view_related_products(self):
        ids = [line.product_id.id 
               for line in self.order_line]
        tree_res = self.env['ir.model.data'].get_object_reference('code_residing_module_name', 'Your_tree_view_id')
        tree_id = tree_res and tree_res[1] or False
        form_res = self.env['ir.model.data'].get_object_reference('code_residing_module_name', 'Your_form_view_id')
        tree_id = form_res and form_res[1] or False
    
        return{
            'name'          :   ('View Chosen Products'), # Nama dari tabel pop up
            'type'          :   'ir.actions.act_window',
            'view_type'     :   'form', #Tampilan pada tabel pop-up
            'view_mode'     :   'tree,form', # Menampilkan bagian yang di pop up, tree = menampilkan tabel tree nya utk product 
            'res_model'     :   'product.product', #Menampilkan tabel yang akan di show di pop-up screen
            'target'        :   'new', # Untuk menjadikan tampilan prduct yang dipilih menjadi pop-up table tampilan baru, jika dikosongin maka tidak muncul pop-up namun muncul halaman baru.
            'views'         :   [(tree_id, 'tree'),(form_id, 'form')]
            'domain'        :   [('id','in',ids)] #Filter id barang yang ditampilkan
            }
    

    【讨论】:

    • 我应该在哪里添加该视图?
    • 当我尝试您的代码时,缅甸先生,我不明白应该填写哪个模块以及哪个 id。
    • 您当前的模块名称和您的树视图_id,请查看编辑后的答案
    【解决方案2】:

    解决了。我只需要在“视图模式”上添加一个“表单”:Python文件中的“树,表单”。

    因此它会自动向您显示客户在同一表单视图中选择的产品的详细信息。因此 Python 文件将如下所示:

    from openerp import models, api, fields, _ 
    
    class task_04 (models.Model):
    
        #Inherit dari model purchase.order
        _inherit    =   "purchase.order"
    
        @api.multi
        def action_view_related_products(self):
            ids = [line.product_id.id 
                   for line in self.order_line]
            return{
                'name'          :   ('View Chosen Products'), # Nama dari tabel pop up
                'type'          :   'ir.actions.act_window',
                'view_type'     :   'form', #Tampilan pada tabel pop-up
                'view_mode'     :   'tree', # Menampilkan bagian yang di pop up, tree = menampilkan tabel tree nya utk product 
                'res_model'     :   'product.product', #Menampilkan tabel yang akan di show di pop-up screen
                'target'        :   'new', # Untuk menjadikan tampilan prduct yang dipilih menjadi pop-up table tampilan baru, jika dikosongin maka tidak muncul pop-up namun muncul halaman baru.
                'view_id'       :   False,
                'domain'        :   [('id','in',ids)] #Filter id barang yang ditampilkan
                }
    
    task_04()
    

    【讨论】:

    • 感谢您发布自我回答。但请不要在标题中添加 [已解决] - 您可以单击与这些答案之一相邻的刻度线,使其变为绿色。
    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多