在openerp中覆盖类的搜索功能时发生错误

时间:2013-05-16 04:19:01

标签: python openerp

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

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)]}),

         } 

1 个答案:

答案 0 :(得分:0)

您需要从xml代码传递它。 project.project应该是很多2,并在xml文件中添加context =“{'test':'yes'}”这样的上下文,你可以像这样定义这个字段:

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

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

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