覆盖openerp中的类的搜索功能时发生错误 [英] Error occured while overriding the search function of a class in openerp

查看:87
本文介绍了覆盖openerp中的类的搜索功能时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图覆盖类'project'的搜索功能来过滤项目.但是它没有给出我需要的列表.它只是从模型中加载所有值.我需要从那里传递上下文.下面是我的代码

I tried to override search function of class 'project' to filter the projects.But its not give a list, which i need. It just load all value from the model . From where I need to pass the context.Below given is my code

class project(osv.osv):
_name = "project.project"
_description = "Project"    
_inherits = {'account.analytic.account': "analytic_account_id",
             "mail.alias": "alias_id"}

def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
    if user == 1:
        return super(project, self).search(cr, user, args, offset=offset, limit=limit, order=order, context=context, count=count)
    if context and context.get('user_preference'):
            cr.execute("""SELECT project.id FROM project_project project
                       LEFT JOIN account_analytic_account account ON account.id = project.analytic_account_id
                       LEFT JOIN project_user_rel rel ON rel.project_id = project.id
                       WHERE (account.user_id = %s or rel.uid = %s)"""%(user, user))                
            return [(r[0]) for r in cr.fetchall()]
    return super(project, self).search(cr, user, args, offset=offset, limit=limit, order=order,
        context=context, count=count)

_columns = {
            'members': fields.many2many('res.users', 'project_user_rel', 'project_id', 'uid', 'Project Members',
        help="Project's members are users who can have an access to the tasks related to this project.", states={'close':[('readonly',True)], 'cancelled':[('readonly',True)]}),

         } 

推荐答案

您需要从xml代码传递它.它与project.project应该是many2one,并在xml文件中添加诸如context ="{'test':'yes'}"之类的上下文,您可以在其中定义以下字段:

You need to pass it from xml code. It should be many2one with project.project and add the context like context="{'test': 'yes'}" in xml file where you define this field like this:

<field name="project_id" context="{'test': 'test'}"/>

通过覆盖project.project的搜索方法,请检查此上下文.现在,您应该在搜索方法中获得此上下文.

By overriding search method of project.project, check this context. Now you should get this context in search method.

如果获得此上下文,请触发查询,相应地获取结果,并将其作为ID列表返回.如果没有得到,请返回代码中指定的project.project的超级方法.另外,根据需要删除如果user == 1的条件,则不需要此条件,否则它将包含所有项目列表.

If you get this context, fire your query, get the result accordingly it and return it as list of ids. If you don't get, return super method of project.project as specified in the code. Also, remove the condition of if user==1 as per your need, you do not require it otherwise it will come up with all list of projects.

这篇关于覆盖openerp中的类的搜索功能时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆