具有值user.id的Odoo v9域过滤器会引发未定义用户的错误 [英] Odoo v9 domain filter with value user.id throws error that user is not defined

查看:65
本文介绍了具有值user.id的Odoo v9域过滤器会引发未定义用户的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个正在运行的V9 odoo实例.在将域过滤器与评估值一起使用的任何地方,都会引发错误.

We have an instance of V9 odoo running. Anywhere that a domain filter is used with an evaluated value, an error is thrown.

例如,在res.users搜索视图上,我创建了一个简单的域过滤器:

As an example, on the res.users searchview I have created a simple domain filter:

[('id', '=', user.id)]

应用此过滤器时,会引发以下错误:

When applying this filter the following error is thrown:

错误:无法评估搜索条件: {代码":400,消息":评估错误",数据":{类型":"local_exception",调试":本地评估失败\ nNameError:未定义名称'user'\ n \ n {\"domains \":[[],\"['id','=',user.id] \"],\"contexts \":[{\"lang \":\"en_GB \" ,\"tz \":\亚洲/西贡",\"uid \":566,\"params \":{\"action \":69,\"page \":0,\"limit \ :80,\" view_type \:\"列表\,\"模型\:\" res.users \,\" menu_id \:79,\" _ push_me \:false},\" search_default_no_share \:1},{},\" {} \],\" group_by_seq \:[\" {} \]}"}}

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\":[\"{}\"]}"}}

无论使用什么odoo系统值,都会发生这种情况.例如:

This occurs no matter what odoo system values are used. For example:

  • user.partner_id
  • user.name
  • user.id

唯一不会出错的错误是uid,即

The only one that does not through an error is uid, i.e.

[('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.

我尝试过以XML添加域过滤器,并在技术设置中使用高级过滤器无济于事.

I have tried adding the domain filter in XML and using the advanced filters in the technical settings to no avail.

我曾在两个单独的v9实例中尝试过此方法,但结果相同.

I have tried this in two separate v9 instances with the same result.

尝试使用user.id或uid在如下所示的v11的新实例中添加任何域过滤器,将返回格式不正确的域过滤器"错误.

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.

[["name","=",user.id]]

任何有关我做错事情的线索都会受到欢迎.

Any clues on what I am doing wrong would be welcomed.

推荐答案

问题是user是一个变量,不幸的是,该变量仅在写入特定模型(例如ir.rule)的记录时才可用(在这里您可以使用domain_force字段中的user.

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).

因此该变量在搜索视图中不存在,这就是您收到错误的原因.

So that variable doesn't exist in a search view, that's why you get the error.

看看规则"官方文档: https://www. odoo.com/documentation/8.0/reference/security.html

Take a look at the Rules official documentation: https://www.odoo.com/documentation/8.0/reference/security.html

用于检查给定记录是否符合规则的域(并且是 可访问性)或不可访问(并且不可访问).域是 在上下文中使用两个变量进行评估:user是当前用户的 记录和时间是时间模块

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

所以您正在寻找的解决方案是这个:

So the solution you're looking for is this one:

crm.opportunity模型中创建一个名为my_division的新计算字段:

Create a new computed field named my_division in crm.opportunity model:

@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)]"/>

应该可以.让我知道.

编辑

有时,当您创建一个存储在数据库中的计算字段时,它的行为与预期不符(它将停止重新计算自身).当我厌倦了为此而苦苦挣扎时,我会执行以下技巧(我一点都不喜欢,但是...我需要继续).

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).

您可以保留我上面写给您的过滤器,但是您必须在crm.opportunity模型中进行一些修改:

You can preserve the filter I wrote you above, but you have to modify a couple of things in the crm.opportunity model:

首先,将my_division设置为非计算字段.然后,修改crm.opportunity createwrite ORM方法(请注意super-不要完全写CrmOpportunity,写您为Python类选择的名称-):

First, make my_division a non-computed field. 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-):

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.

这篇关于具有值user.id的Odoo v9域过滤器会引发未定义用户的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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