简体   繁体   中英

OpenERP 7 - How to switch model/formView on a treeview click?

One of the big requirements of my company is the ability to search both Customers and Leads at the same time (Sort of a unified search).

I have figured out how to do that by creating a class as follows:

from osv import fields, osv
from openerp import tools
from tools.translate import _
import netsvc

class universal(osv.osv):


    _name = "universal_search.model"
    _description = "Universal Search"
    _auto = False
    _columns = {
    'name': fields.char('Name', size=128, readonly=True),
    'phone': fields.char('Phone', size=128, readonly=True),
    'city': fields.char('City', size=128,readonly=True),
    'state': fields.char('State', size=128,readonly=True),
    'country': fields.char('Country', size=128,readonly=True),
    'zip': fields.char('Postal Code', size=128,readonly=True),
    'email': fields.char('E-Mail', size=128,readonly=True),
    'type': fields.char('Type', readonly=True)
    }
    _order = 'type asc, name asc'



    def init(self, cr):
    tools.sql.drop_view_if_exists(cr, 'universal_search_model')
    cr.execute("""
        CREATE OR REPLACE VIEW universal_search_model AS (
            select res_partner.id,res_partner.name,phone,city,zip,email,res_country_state.name as state,res_country.name as country,CASE WHEN is_company=TRUE THEN 'Customer' ELSE 'Contact' END as type
            from res_partner
            left join res_country_state on res_partner.state_id = res_country_state.id
            left join res_country on res_partner.country_id = res_country.id
            WHERE customer = TRUE
        UNION ALL
            select crm_lead.id,crm_lead.name,phone,city,zip,email_from,res_country_state.name as state,res_country.name as country,'Lead' as type
            from crm_lead
            left join res_country_state on crm_lead.state_id = res_country_state.id
            left join res_country on crm_lead.country_id = res_country.id
        )

    """)
universal()

Now my problem is the ability to switch which view gets selected when a customer clicks on one of the records. Obviously, if a user clicks on Lead record they should go to the lead form and ditto for the Customer.

I've seen examples using priority levels and context based switching, but none of these seem to actually address what I am trying to do.

EDIT FOR CLARITY:

Essentially, I have 2 kinds of records being pulled. Based on their type I need to pull a different inherited form view. If the record is coming from Customers I need to inherit and display: base.view_partner_form. If the record is a lead, I need to display:crm.crm_case_form_view_leads

Any help would be appreciated.

Here is how you could do it

  1. by linking each records to their specific category lead or customer view type like this

    here your records variable

and this object "your_object_returns_the_view"

your_object_returns_the_view((self, cr, uid, ids, context=None):
        .................................
        ................................
return {
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': 'here the model',
        'type': 'ir.actions.act_window',
        'target': 'new',
        'name': name,
        'context': context
       }
  1. or in simple way you can merge the two records by inheriting one object to another (crm_lead and res_partener) and put the all in the same tree view and you can write searching in xml with their record value (I didn't try this but I think it is possible)

Additional

Write the function (which returns the view respectively to records type) "universal"

class universal(osv.osv):
     .
     .
def your_object_returns_the_view_for_leads(self, cr, uid, ids, context=None):
     //here you need to write certain conditions to identify each records (lead     

     //or customer) and to return two view form
    return {
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': ' universal',
        'type': 'ir.actions.act_window',
        'target': 'new',
        'name': name,
        'context': context
      }

Now in your tree view here what you do

    <tree>
    <a name="your_object_returns_the_view_for_leads" type="object"><field name="field_name_from_universal" /></a>
    </tree>

whenever this record clicked it returns in its respective view form

noble_man definitely pointed me in the right direction. Here is what my solution was based on what he gave me as a starting point. There is still a little polishing left though, but it is "functional" now.

Here is my view:

<record id="universal_tree" model="ir.ui.view">
    <field name="name">universal_search.tree</field>
    <field name="model">universal_search.model</field>
    <field name="context">{"record_type":"installation"}</field>
    <field name="arch" type="xml">
    <tree string="Results" create="false" delete="false">
        <button type="object" string="Open" name="open_full_record" icon="gtk-go-forward" context="{'id':id,'type':type}"/>
        <field name="type"/>
        <field name="name"/>
        <field name="phone"/>
        <field name="city"/>
        <field name="zip"/>
        <field name="country"/>

    </tree>
    </field>
</record>

And now for my function.

def open_full_record(self, cr, uid, ids, context=None):


    obj = self.browse(cr, uid, ids, context)

    if context['type'] == 'Customer':
        model = 'res.partner'
    elif context['type'] == 'Lead':
        model = 'crm.lead'
    else:
        return False

    return {
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': model,
        'type': 'ir.actions.act_window',
        'target': 'self',
        'res_id': context['id'],
        'context': context,
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM