gpt4 book ai didi

python - 如何使用动态字段odoo-10过滤 TreeView

转载 作者:太空狗 更新时间:2023-10-30 02:54:39 25 4
gpt4 key购买 nike

我将“location_id”字段添加到 res.user 以将用户连接到特定位置

类 ResUser(模型。模型): _inherit='res.users'

current_location_id=fields.Many2one('physical.location',string="Current Loction")
allowed_location_ids=fields.Many2many('physical.location',string="Allowed Location")

我只想过滤 current_location_id = user_current_location_id 的访问

<filter string="My visits" name="filter_my_visits"
domain="[('current_location_id','=',current_location_id)]"/>

给出了这个错误信息

    Uncaught Error: Failed to evaluate search criterions: 
{"code":400,"message":"Evaluation Error","data":{"type":"local_exception","debug":"Local evaluation failure\nNameError: name 'current_location_id' is not defined\n\n{\"domains\":[[],\"[('current_location_id','=',current_location_id)]\"],\"contexts\":[{\"lang\":\"en_US\",\"tz\":false,\"uid\":1,\"params\":{\"action\":354,\"min\":1,\"limit\":80,\"view_type\":\"list\",\"model\":\"visit.visit\",\"menu_id\":241,\"_push_me\":false}},{}],\"group_by_seq\":[]}"}}

View 中唯一可用的可直接调用的字段是 uid lang ... 等所以我尝试覆盖读取方法来更新上下文,但它似乎没有在 View 中更新,仍然给出未定义的“current_location_id”

    @api.multi
def read(self, fields=None, load='_classic_read'):
ctx = self.env.context.copy()
ctx.update({ 'user_current_location_id':self.env.user.current_location_id.id})

return super(Visit,self.with_context(ctx)).read(fields=fields,load=load)

访问 TreeView

 <record id="visit_visit_tree_view" model="ir.ui.view">
<field name="name">visit.visit.tree</field>
<field name="model">visit.visit</field>
<field name="arch" type="xml">
<tree string="Visits">
<field name="name" />
<field name="date"/>
<field name="visit_type" />
<field name="patient_id"/>
<field name="hcp_id"/>
<field name="current_location_id" />
<field name="user_current_location_id"/>
<field name="severity"/>
<field name="state"/>
</tree>
</field>
</record>

访问 Action

<record id="visit_visit_act" model="ir.actions.act_window">
<field name="name">Visit</field>
<field name="res_model">visit.visit</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="context">{'search_default_filter_my_visits':1}</field>
<field name="search_view_id" ref="view_visit_filter"/>
</record>

这是我做的:

 @api.multi
def vsit_action(self):
ctx = self.env.context.copy()

ctx.update({'search_default_current_location_id': self.env.user.current_location_id.id,
'user_current_location_id': self.env.user.current_location_id.id})
return {
'name': 'Visit',
'view_type': 'form',
'view_mode': 'tree,form',
'res_model': 'visit.visit',
'type': 'ir.actions.act_window',
'context': dict(ctx),
'search_view_id': self.env.ref('eroyal_visit.view_visit_filter').id
}

过滤器:

    <filter string="My visits" name="filter_my_visits"
domain="[('current_location_id', '=', context.get('user_current_location_id'))]"/>


the visit action
<record id="visit_visit_act" model="ir.actions.server">
<field name="name">Visit</field>
<field name="model_id" ref="model_visit_visit"/>
<field name="state">code</field>
<field name="code">
action = env['visit.visit'].vsit_action()


</field>
</record>

default_search_current_id 工作正常,但过滤器 filter_my_visits 确实使用 current_location_id=none 过滤 View ,同时在上下文中正确设置坦克

最佳答案

应该可以通过一些变通方法实现。而不是使用 ir.actions.act_window 尝试使用 ir.actions.server 应该返回一个窗口操作。为什么?因为您需要有可能将用户的位置 ID 放入该窗口操作的上下文中。我只是通过使用带有代码的服务器操作才看到这种可能性。

只需用位置 ID 填充窗口操作的上下文:

ctx = dict(env.context,
{'user_location_id': env.user.current_location_id.id,
'search_default_filter_my_visits': 1}
return {
'name': 'Visit',
'view_type': 'form',
'res_model': 'visit.visit',
'type': 'ir.actions.act_window',
'context': ctx,
'search_view_id': env.ref('my_module.view_visit_filter');
}

对于您的过滤器:

<filter string="My visits" name="filter_my_visits"
domain="[('current_location_id', '=', context.get('user_location_id')]"/>

关于python - 如何使用动态字段odoo-10过滤 TreeView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45056031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com