0
votes

I have create a list view with the help of psql query with _auto = False. So there is not model registered against this. Now i want to customize this click event on the records that whenever a user click on any records i want to pass an order_id and then redirect user to that particular order detail screen.

Edit

View

<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")

Action

<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>

Debugger

Self._context

self._context

Self

Self

2
Why did you do so without any Odoo model definition?travisw
I doesn't mean that i do have a model defination same as the query returns. I was saying that _auto=False doesn't create table.Ancient

2 Answers

0
votes

I think there are a couple of ways this could possibly work. I'm not 100% certain either of them will, but I'll give you the general ideas.

XML Only Solution

George has the main idea correct - you need to create an action to manage what you're wanting to do.

The important distinction is that you seem to want the list view to display one model (amgl.dashboard) but the form view to display another (sale.order)

Normally, I would suggest you create a button and display it in the list view to take the user directly to the sales order form. However, you may also be able to use a standard act_window action with the help of src_model.

Example from core:

Odoo uses the button below to invoke the action, which results in the user being able to go to the stock.location form view and go to the product.product list. You are essentially wanting to do the opposite of this with different models and without the button.

<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 Solution:

Assuming your view_record method is being called already, you can return the "View Sales Order form" action directly from the method itself.

# 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': {},
        }
0
votes

When you click a record on a listview an action is executed that moves you from the listview to a `formview.

There always exists a default action but if you want to pass some extra parameters you have to define your own. For a concrete example take a look at the sale.action_quotations action. This action moves the user from view1 to view2 (list to form for example)

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

This action is responsible for moving the user in Quotations between views. See how the context is passed. You only need tree,form on the view_mode here.