问题
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)]}),
}
回答1:
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'}"/>
By overriding search method of project.project, check this context. Now you should get this context in search method.
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.
来源:https://stackoverflow.com/questions/16578933/error-occured-while-overriding-the-search-function-of-a-class-in-openerp