1

嗨,我继承了 CRM 模块。我继承了客户模型并将 redirect_partner_form 覆盖为

from osv import fields, osv
from IPython.Debugger import Tracer; debug_here = Tracer()
class worldcable_customer(osv.osv):
   _name = "res.partner"
   _description = "worldcable customer"
   _inherit =  "res.partner"
   _columns = {
    'id': fields.integer('ID', readonly=True),
    'connection_info': fields.one2many('worldcable.connection.info', 'partner_id', 'Connection Info'),
    }

    def redirect_partner_form(self, cr, uid, partner_id, context=None):
        debug_here()
        search_view = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'worldcable2',   'view_worldcable_customer_form')
    value = {
        'domain': "[]",
        'view_type': 'form',
        'view_mode': 'form,tree',
        'res_model': 'res.partner',
        'res_id': int(partner_id),
        'view_id': False,
        'context': context,
        'type': 'ir.actions.act_window',
        'search_view_id': search_view and search_view[1] or False
    }
    debug_here()
    return value
    worldcable_customer()

并在向导中调用它,

from osv import osv, fields
from tools.translate import _
class crm_lead2partner(osv.osv_memory):
""" Converts lead to partner """
_name = 'worldcable.crm.connection2partner'
_description = 'Connection to Partner'

_columns = {
    'action': fields.selection([('exist', 'Link to an existing partner'), \
                                ('create', 'Create a new partner')], \
                                'Action', required=True),
    'partner_id': fields.many2one('res.partner', 'Partner'),
}
def view_init(self, cr, uid, fields, context=None):
    """
    This function checks for precondition before wizard executes
    """
    if context is None:
        context = {}
    model = context.get('active_model')
    model = self.pool.get(model)
    rec_ids = context and context.get('active_ids', [])
    for this in model.browse(cr, uid, rec_ids, context=context):
        if this.partner_id:
            raise osv.except_osv(_('Warning !'),
                    _('A partner is already defined.'))

def _select_partner(self, cr, uid, context=None):
    if context is None:
        context = {}
    lead = self.pool.get('crm.lead')
    partner = self.pool.get('res.partner')
    lead_ids = list(context and context.get('active_ids', []) or [])
    if not len(lead_ids):
        return False
    this = lead.browse(cr, uid, lead_ids[0], context=context)
    # Find partner address matches the email_from of the lead
    res = lead.message_partner_by_email(cr, uid, this.email_from, context=context)
    partner_id = res.get('partner_id', False)      
    # Find partner name that matches the name of the lead
    if not partner_id and this.partner_name:
        partner_ids = partner.search(cr, uid, [('name', '=', this.partner_name)], context=context)
        if partner_ids and len(partner_ids):
           partner_id = partner_ids[0]
    return partner_id

def default_get(self, cr, uid, fields, context=None):
    """
    This function gets default values
    """
    res = super(crm_lead2partner, self).default_get(cr, uid, fields, context=context)        
    partner_id = self._select_partner(cr, uid, context=context)

    if 'partner_id' in fields:
        res.update({'partner_id': partner_id})
    if 'action' in fields:
        res.update({'action': partner_id and 'exist' or 'create'})

    return res

def open_create_partner(self, cr, uid, ids, context=None):
    """
    This function Opens form of create partner.
    """
    view_obj = self.pool.get('ir.ui.view')
    view_id = view_obj.search(cr, uid, [('model', '=', self._name), \
                                 ('name', '=', self._name+'.view')])
    return {
        'view_mode': 'form',
        'view_type': 'form',
        'view_id': view_id or False,
        'res_model': self._name,
        'context': context,
        'type': 'ir.actions.act_window',
        'target': 'new',
    }

def _create_partner(self, cr, uid, ids, context=None):
    """
    This function Creates partner based on action.
    """
    if context is None:
        context = {}
    lead = self.pool.get('crm.lead')
    lead_ids = context and context.get('active_ids') or []
    data = self.browse(cr, uid, ids, context=context)[0]
    partner_id = data.partner_id and data.partner_id.id or False
    return lead.convert_partner(cr, uid, lead_ids, data.action, partner_id, context=context)

def make_partner(self, cr, uid, ids, context=None):
    """
    This function Makes partner based on action.
    """
    # Only called from Form view, so only meant to convert  one Lead. 
    lead_id = context and context.get('active_id') or False
    partner_ids_map = self._create_partner(cr, uid, ids, context=context)
    return self.pool.get('res.partner').redirect_partner_form(cr, uid, partner_ids_map.get(lead_id,  False), context=context)

crm_lead2partner()

但它从 Base res.partner 调用原始形式。我已经从 db 检查了 search_view_id,我的视图的 id 相同。但我不知道有什么错误。但令人惊讶的是,当我看到对客户端的响应时,它就像正在返回 connection_info 表,这意味着它正在调用我的视图。但我不知道为什么它从基地显示 res.partner 表格。

DEBUG_RPC_ANSWER:rpc.result:{'domain': '[]', 'view_type': 'form', 'res_model': 'res.partner',   'view_id': False, 'views': [(False, 'tree'), (57L, 'form')], 'search_view_id': 710, 'view_mode': 'form,tree', 'res_id': 57, 'context': {'lang': u'en_US', 'tz': False, 'active_model': 'crm.lead', 'section_id': False, 'search_default_current': 1, 'active_ids': [35L], 'active_id': 35L}, 'type': 'ir.actions.act_window'}

我注意到其中有地址错误

ERROR:tools.expr_eval:{'address': address}
Traceback (most recent call last):
File "/home/noaman/projects/openerp/repository-openerp/Sid/client/bin/tools/__init__.py", line 52,  in   expr_eval
temp = eval(string, context)
File "<string>", line 1, in <module>
NameError: name 'address' is not defined

我的客户 xml 如下

<openerp>
<data>
<record model="ir.ui.view" id="view_worldcable_customer_form">
        <field name="name">worldcable.partner.form</field>
        <field name="model">res.partner</field>
        <field name="type">form</field>
        <field name="arch" type="xml">
            <form string="Worldcable Customers" col='1'>
                <group col="6" colspan="4">
                    <group colspan="5" col="6">
                        <field name="name" select="1"/>
                        <field name="ref" groups="base.group_extended"/>
                        <field domain="[('domain', '=', 'partner')]" name="title" size="0"  groups="base.group_extended"/>
                        <field name="lang"/>
                    </group>
                    <group colspan="1" col="2">
                        <field name="customer" select="1" readonly="1"/>
                         <!--                            <field name="employee"/>-->
                    </group>
                </group>
                <notebook colspan="4">
                    <page string="Connection Info">
                     <field colspan="4" mode="form,tree" name="connection_info" nolabel="1" select="1" height="260">
                        <form string="Connection Info">
                            <group colspan="4" col="4">
                                <field name="mac_address"/>
                                <field name="telephone_no"/>
                                <field name="tv_provider"/>
                                <field name="internet_provider"/>
                                <field name="phone_provider"/>
                                <field name="circuit_id"/>                                    
                                <field name="iigo_number"/>
                                <field name="dsl_no"/>
                                <field name="exchange"/>
                                <field name="service_type"/>   
                            </group>
                        </form>
                        <tree string="Connection Info">
                            <field name="mac_address"/>
                            <field name="circuit_id"/>
                            <field name="iigo_number"/>
                            <field name="dsl_no"/>
                            <field name="exchange"/>
                            <field name="tv_provider"/>
                            <field name="phone_provider"/>
                            <field name="internet_provider"/>
                        </tree>                            
                     </field>
                    </page>
                    <page string="General">
                        <field colspan="4" mode="form,tree" name="address" nolabel="1" select="1" height="260">
                            <form string="Partner Contacts">
                                <group colspan="4" col="6">
                                    <field domain="[('domain', '=', 'contact')]" name="title" size="0"/>
                                    <field name="first_name" colspan="2" string="First Name"/>
                                    <field name="middle_name" colspan="1" string="Middle Name"/>
                                    <field name="last_name" colspan="1" string="Last Name"/>   
                                </group>
                                <newline/>
                                <group colspan="2" col="4">

                                    <separator string="Postal Address" colspan="4" col="4" />
                                    <field name="type" string="Type" colspan="2"/>
                                    <field name="floor" colspan="2"/>
                                    <field name="zip"/>
                                    <field name="street" colspan="4"/>
                                    <field name="street2" colspan="4"/>
                                    <field name= "apartment" colspan="2"/>
                                    <field name="city"/>
                                    <field name="state_id"/>
                                    <field name="country_id" completion="1"/>
                                </group>
                                <group colspan="2" col="2">
                                    <separator string="Communication" colspan="2" col="2" />
                                    <field name="phone"/>
                                    <field name="mobile"/>
                                    <field name ="other_no"/>
                                    <field name="fax"/>
                                    <field name="email" widget="email"/>
                                </group>
                            </form>
                            <tree string="Partner Contacts">
                                <field name="name"/>
                                <field name="zip"/>
                                <field name="city"/>
                                <field name="country_id"/>
                                <field name="phone"/>
                                <field name="email"/>
                            </tree>
                        </field>
                    </page>
                    <page string="Sales &amp; Purchases">
                        <separator string="General Information" colspan="4"/>
                        <field name="user_id"/>
                        <field name="active" groups="base.group_extended"/>
                        <field name="website" widget="url"/>
                        <field name="date"/>
                        <field name="parent_id" groups="base.group_extended"/>
                        <field name="company_id" groups="base.group_multi_company" widget="selection"/>
                        <newline/>
                    </page>
                    <page string="History" groups="base.group_extended" invisible="True">
                    </page>
                    <page string="Notes">
                        <field colspan="4" name="comment" nolabel="1"/>
                    </page>

                </notebook>
            </form>
        </field>
  </record>
  <record id="act_my_worldcable_customer_form" model="ir.actions.act_window">
        <field name="name">Worldcable Customer</field>
        <field name="res_model">res.partner</field>
        <field name="view_type">form</field>
        <field name="view_mode">tree,calendar,form</field>
        <field name="context">{'search_default_current':1}</field>
        <field name="help">Worldcable Customer Search</field>
        <field name="view_id" ref="view_worldcable_customer_form"/>
  </record>
  <menuitem id="menu_myview_worldcable_customer" parent="base.myview_menu" name="Customers" icon="terp-partner" action="act_my_worldcable_customer_form" groups="base.group_extended,base.group_sale_salesman" sequence="2"/>
 </data>
 </openerp>

有人可以帮我吗,我怎么称呼我的表格而不是基本表格。我已经尝试了所有方法。提前致谢。

4

1 回答 1

2

操作定义中的search_view_id字段用于指定search要使用的form视图,而不是顾名思义的视图。如果您想使用特定的表单视图,您应该使用该view_id字段(用于指定要打开的主视图,通常是 aformtreeone)。顺便说一句,OpenERP 操作定义中的所有视图选择字段都可以通过指定一个字段来覆盖:可以使用默认视图的views有序对列表。这是框架自动添加到常规操作上的计算字段,但可以手动添加到 Python 方法返回的自定义操作中。(view_id, view_mode)view_idFalse

以下是您可以在 Python 方法中执行此操作的方法:

# assuming partner_id, context, form_view_id are defined here
return {
    'type': 'ir.actions.act_window',
    'view_type': 'form',
    'view_mode': 'form,tree',
    'res_model': 'res.partner',
    'res_id': int(partner_id),
    'context': context,
    'view_id': form_view_id, 

    # optionally, you could refine by specifying the 'views' explicitly
    'views': [(form_view_id, 'form'),  # open my form view first,
              (False, 'tree')]         # then default tree view
}

你会在官方插件的源码中找到很多类似的例子,搜索代码返回'views''view_id'

现在有一些与您正在尝试做的事情相关的附带问题,除了解决您当前的问题之外,您可能还想回答这些问题。

  1. 在 OpenERP 中修改现有视图的最简单方法是继承它。乍一看,您似乎只是在尝试在合作伙伴表单视图上创建一个新选项卡“连接信息”。简单地创建一个挂钩到<notebook>父视图元素的继承视图并在其中添加一个额外的元素是微不足道的(而且简单得多) <page>。如果您不想在所有情况下都显示选项卡,您可以使用特殊attrs属性向页面添加可见性修饰符。
    因此问题是:你为什么不在这里使用这种技术呢?

  2. 当您不想继承现有视图时(因为新视图完全不同),第二个最简单的方法是创建一个相同类型的新视图并赋予它更高的优先级(较低的priority字段值)。这将在需要此视图类型的任何地方自动替换默认视图。唯一不起作用的情况是打开视图的操作请求特定的 view_id。
    看来你可以在这里做,所以第二个问题是:为什么不在这里使用其他技术呢?

于 2012-10-01T08:30:56.653 回答