[英]Odoo v9 domain filter with value user.id throws error that user is not defined
We have an instance of V9 odoo running. 我们有一个V9 odoo运行的实例。 Anywhere that a domain filter is used with an evaluated value, an error is thrown. 在将域过滤器与评估值一起使用的任何地方,都会引发错误。
As an example, on the res.users searchview I have created a simple domain filter: 例如,在res.users搜索视图上,我创建了一个简单的域过滤器:
[('id', '=', user.id)]
When applying this filter the following error is thrown: 应用此过滤器时,将引发以下错误:
Error: Failed to evaluate search criterions: {"code":400,"message":"Evaluation Error","data":{"type":"local_exception","debug":"Local evaluation failure\\nNameError: name 'user' is not defined\\n\\n{\\"domains\\":[[],\\"['id', '=', user.id]\\"],\\"contexts\\":[{\\"lang\\":\\"en_GB\\",\\"tz\\":\\"Asia/Saigon\\",\\"uid\\":566,\\"params\\":{\\"action\\":69,\\"page\\":0,\\"limit\\":80,\\"view_type\\":\\"list\\",\\"model\\":\\"res.users\\",\\"menu_id\\":79,\\"_push_me\\":false},\\"search_default_no_share\\":1},{},\\"{}\\"],\\"group_by_seq\\":[\\"{}\\"]}"}} 错误:无法评估搜索条件:{“代码”:400,“消息”:“评估错误”,“数据”:{“类型”:“ local_exception”,“调试”:“本地评估失败\\ nNameError:名称'用户'未定义\\ n \\ n {\\“域\\”:[[],\\“ ['id','=',user.id] \\”],\\“上下文\\”:[{\\“ lang \\ “:\\” EN_GB \\ “\\ ”TZ \\“:\\ ”亚洲/胡志明市\\“,\\ ”UID \\“:566,\\ ”PARAMS \\“:{\\ ”动作\\“:69 \\” 页\\ “:0,\\” 限制\\ “:80,\\” 的view_type \\ “:\\” 目录\\ “\\ ”模型\\“:\\ ”res.users \\“,\\ ”menu_id \\“:79,\\” _ push_me \\ “:假},\\” search_default_no_share \\ “:1},{},\\”{} \\ “],\\” group_by_seq \\ “:[\\”{} \\ “]}”}}
This occurs no matter what odoo system values are used. 无论使用什么odoo系统值,都会发生这种情况。 For example: 例如:
The only one that does not through an error is uid, ie 唯一不会出错的是uid,即
[('id', '=', uid)]
The purpose of accessing user is to access further values related to the current user. 访问用户的目的是访问与当前用户有关的其他值。 The entire code for the domain filter I am trying to create is the following: 我尝试创建的域过滤器的整个代码如下:
<record id="crm_opportunity_search_view" model="ir.ui.view">
<field name="name">crm.opportunity.search.view</field>
<field name="model">crm.opportunity</field>
<field name="arch" type="xml">
<search string="Opportunities">
<field name="name" filter_domain="[('name','ilike',self)]"/>
<filter string="My Division" name="my_division" domain="[('owner_id.business_unit_id.id', '=', user.partner_id.business_unit_id.id)]"/>
</search>
</field>
</record>
"My division" is an available filter in the filters menu from opportunities. “我的部门”是机会中过滤器菜单中的可用过滤器。 However, when selected throws an error that "user" is not defined. 但是,当选择引发错误时,未定义“用户”。
I have tried adding the domain filter in XML and using the advanced filters in the technical settings to no avail. 我尝试在XML中添加域过滤器,并在技术设置中使用高级过滤器无济于事。
I have tried this in two separate v9 instances with the same result. 我已经在两个单独的v9实例中尝试了相同的结果。
Trying to add any domain filter in a new instance of v11 such as below, using user.id or uid returns a "domain filter not properly formed" error. 尝试使用以下user.id或uid在v11的新实例中添加任何域过滤器,将返回“域过滤器格式不正确”错误。
[["name","=",user.id]]
Any clues on what I am doing wrong would be welcomed. 任何关于我做错事情的线索都会受到欢迎。
The problem is that user
is a variable which unfortunately is only available when writing records from specific models, like for example ir.rule
(here you can use user
in the domain_force
field). 问题是, user
是一个变量,不幸的是,该变量仅在从特定模型写入记录时才可用,例如ir.rule
(您可以在domain_force
字段中使用user
)。
So that variable doesn't exist in a search view, that's why you get the error. 因此该变量在搜索视图中不存在,这就是为什么会出现错误的原因。
Take a look at the Rules official documentation: https://www.odoo.com/documentation/8.0/reference/security.html 看看规则官方文档: https : //www.odoo.com/documentation/8.0/reference/security.html
A domain used to check whether a given record matches the rule (and is accessible) or does not (and is not accessible). 用于检查给定记录是否符合规则(可访问)或不匹配(且不可访问)的域。 The domain is evaluated with two variables in context: user is the current user's record and time is the time module 在上下文中使用两个变量评估域:user是当前用户的记录,time是时间模块
So the solution you're looking for is this one: 因此,您正在寻找的解决方案是:
Create a new computed field named my_division
in crm.opportunity
model: 在crm.opportunity
模型中创建一个名为my_division
的新计算字段:
@api.multi
@api.depends('owner_id', 'owner_id.business_unit_id')
def _compute_my_division(self):
for opportunity in self:
if opportunity.owner_id.business_unit_id.id == self.env.user.partner_id.business_unit_id.id:
opportunity.my_division = True
my_division = fields.Boolean(
compute='_compute_my_division',
string='My division',
store=True,
)
Add this field (invisible) to the views (tree, kanban, etc) you can search for. 将此字段(不可见)添加到您可以搜索的视图(树,看板等)中。 Then modify your search filter this way: 然后以这种方式修改搜索过滤器:
<filter string="My Division" name="my_division" domain="[('my_division','=',1)]"/>
That should work. 那应该工作。 Let me know. 让我知道。
EDIT 编辑
Sometimes, when you create a computed field which is stored in the database, it doesn't behave as expected (it stops recalculating itself). 有时,当您创建一个存储在数据库中的计算字段时,它的行为与预期不符(它将停止重新计算自身)。 When I'm fed up with struggling with that, I do the following trick (I don't like it at all but... I need to carry on). 当我厌倦了为此而苦苦挣扎时,我会做以下技巧(我一点都不喜欢,但是……我需要继续)。
You can preserve the filter I wrote you above, but you have to modify a couple of things in the crm.opportunity
model: 您可以保留我上面写的过滤器,但必须在crm.opportunity
模型中进行一些修改:
First, make my_division
a non-computed field. 首先,将my_division
非计算字段。 Then, modify crm.opportunity
create
and write
ORM methods (be careful with the super
-don't write exactly CrmOpportunity
, write the name you chose for the Python class-): 然后,修改crm.opportunity
create
并write
ORM方法(注意super
精确地编写CrmOpportunity
,请编写您为Python类选择的名称-):
my_division = fields.Boolean(
string='My division',
default=False,
)
@api.model
def create(self, vals)
opportunity = super(CrmOpportunity, self).create(vals)
if opportunity.owner_id.business_unit_id.id == self.env.user.partner_id.business_unit_id.id:
opportunity.write({
'my_division': True,
})
return opportunity
@api.multi
def write(self, vals)
update = super(CrmOpportunity, self).write(vals)
for opportunity in self:
if opportunity.owner_id.business_unit_id.id == self.env.user.partner_id.business_unit_id.id and \
opportunity.my_division is False:
opportunity.write({
'my_division': True,
})
elif opportunity.owner_id.business_unit_id.id != self.env.user.partner_id.business_unit_id.id and \
opportunity.my_division is True:
opportunity.write({
'my_division': False,
})
else:
continue
return update
This must work for sure, but it's not very clean. 这肯定可以工作,但是不是很干净。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.