Odoo - 在列表视图中单击事件自定义

时间:2017-08-24 18:01:35

标签: openerp odoo-8 odoo-9 odoo-10 odoo-view

我在_auto = False的psql查询的帮助下创建了一个列表视图。所以没有针对此注册的模型。现在我想在记录上自定义此点击事件,每当用户点击我想传递order_id的任何记录,然后将用户重定向到特定的订单详细信息屏幕时。

修改

查看

<odoo>
<data>
    <record id="amgl_dashboard_tree" model="ir.ui.view">
        <field name="name">Dashboard</field>
        <field name="model">amgl.dashboard</field>
        <field name="arch" type="xml">
            <tree default_order="state desc" decoration-bf ="state in ('expecting','pending','completed')" decoration-info="state=='expecting'" decoration-danger="state=='pending'" decoration-succes="state=='completed'" string="Dashboard" create="false" edit="false">
                <field name="order_id" invisible="1"/>
                <button name="view_record" type="object" string="View Record" custom="click" class="oe_highlight"/>
                <field name="first_name"/>
                <field name="last_name"/>
                <field name="account_number"/>
                <field name="product"/>
                <field name="quantity"/>
                <field name="total_weight"/>
                <field name="state"/>
            </tree>
        </field>
    </record>
</data>
</odoo>

的.py

class Dashboard(models.Model):
_name = 'amgl.dashboard'
_auto = False

@api.multi
def view_record(self):
    print self

@api.model_cr
def init(self):
    tools.drop_view_if_exists(self._cr, 'dashboard')
    self._cr.execute("""
            CREATE or REPLACE VIEW amgl_dashboard AS (

                SELECT 
                    row_number() OVER () AS id,
                    c.name AS first_name,
                    c.last_name AS last_name,
                    c.account_type AS account_type,
                    c.account_number AS account_number,                        
                    (select name from amgl_products where id = ol.products) AS product,
                    ol.quantity AS quantity,
                    (CASE
                    WHEN (select weight_unit from amgl_products where id = ol.products) = 'oz'
                           THEN
                        (select weight_per_piece from public.amgl_products where id = ol.products) * ol.quantity
                    WHEN (select weight_unit from amgl_products where id = ol.products) = 'gram'
                           THEN
                        ((select weight_per_piece from public.amgl_products where id = ol.products) / 28.34952) * ol.quantity
                    WHEN (select weight_unit from amgl_products where id = ol.products) = 'pounds'
                           THEN
                        ((select weight_per_piece from amgl_products where id = ol.products) * 16) * ol.quantity
                    WHEN (select weight_unit from amgl_products where id = ol.products) = 'kg'
                           THEN
                        ((select weight_per_piece from amgl_products where id = ol.products) / 0.02834952) * ol.quantity
                    ELSE 0.0
                    END) AS total_weight,
                    o.state AS state,
                    o.id  AS order_id
                    FROM amgl_order AS o
                    INNER JOIN amgl_customer AS c ON c.id = o.customer_id
                    INNER JOIN amgl_order_line AS ol ON ol.order_id = o.id
            )""")

name = fields.Char()
first_name = fields.Char(string="First Name")
last_name = fields.Char(string="Last Name")
account_type = fields.Char(string="Account Type")
account_number = fields.Char(string="Account Number")
product = fields.Char(string="Product")
quantity = fields.Float(string="Quantity")
total_weight = fields.Float(string="Total Weight")
state = fields.Selection([('expecting', 'Expecting'), ('pending', 'Pending'),
                          ('completed', 'Completed'), ('waiting', 'Waiting For Approval')],
                         'Status', default='expecting')
order_id = fields.Integer(string="Order Id")

行动

<record id="amgl.dashboard_action_window" model="ir.actions.act_window">
        <field name="name">Dashboard</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">amgl.dashboard</field>
        <field name="view_mode">tree</field>
        <field name="help" type="html">
          <p class="oe_view_nocontent_create">
            <!-- Add Text Here -->
          </p><p>
            <!-- More details about what a user can do with this object will be OK -->
          </p>
        </field>
  </record>

调试器

Self._context

self._context

Self

Self

2 个答案:

答案 0 :(得分:0)

当您点击listview上的记录时,会执行一项操作,将您从listview移至`表单视图。

始终存在默认操作,但如果要传递一些额外参数,则必须定义自己的参数。有关具体示例,请查看sale.action_quotations操作。此操作将用户从view1移动到view2(例如列表到表单)

https://github.com/OCA/OCB/blob/10.0/addons/sale/views/sale_views.xml#L518

<record id="action_quotations" model="ir.actions.act_window">
    ...
    <field name="context">{'hide_sale': True}</field>
    ...
</record

此操作负责在视图之间的引文中移动用户。了解上下文的传递方式。您只需tree,form这里的view_mode

答案 1 :(得分:0)

我认为有几种方法可能有效。我不是100%肯定他们中的任何一个会,但我会给你一般的想法。

仅限XML解决方案

乔治的主要想法是正确的 - 你需要创建一个动作来管理你想要做的事情。

重要的区别在于您似乎希望列表视图显示一个模型(amgl.dashboard),但表单视图显示另一个模型(sale.order

通常,我建议您创建一个button并在列表视图中显示它,以便将用户直接带到销售订单表单。但是,您也可以在act_window的帮助下使用标准src_model操作。

来自核心的示例:

Odoo使用下面的按钮来调用操作,这样用户就可以转到stock.location表单视图并转到product.product列表。你基本上想要用不同的模型和没有按钮来做相反的事情。

<button string="Products"
        class="oe_stat_button"
        icon="fa-filter" name="%(act_product_location_open)d" type="action"
        context="{'location_id': active_id}"
        />

<act_window
    id="act_product_location_open"
    name="Products"
    src_model="stock.location"
    res_model="product.product"
    context="{'location': active_id, 
              'search_default_real_stock_available': 1, 
              'search_default_virtual_stock_available': 1,                     
              'search_default_virtual_stock_negative': 1, 
              'search_default_real_stock_negative': 1}"/>

Python解决方案:

假设您已经调用了view_record方法,您可以返回&#34;查看销售订单表单&#34;直接来自方法本身。

# Make sure these imports are called in your file
# If you are on Odoo 9 or earlier, you must use openerp instead of odoo
from odoo import _
from odoo.exceptions import ValidationError


class Dashboard(models.Model):
    _name = 'amgl.dashboard'
    _auto = False 

    @api.multi
    def view_record(self):
        """Return a Window Action to view the Sales Order form"""
        self.ensure_one()
        action = self.env.ref('sale.action_orders')
        form = self.env.ref('sale.view_order_form', False)

        if not (action or form):
            raise ValidationError(_("Sales Orders Action or Form not found!"))
        return {
            'name': action.name,
            'help': action.help,
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'views': [(form_id.id, 'form')]
            'res_model': action.res_model,
            'res_id': self._context.get('id'),
            'target': 'current',
            'context': {},
        }