如何使用动态字段odoo-10过滤树视图 [英] how to filter tree view with dynamic field odoo-10

查看:104
本文介绍了如何使用动态字段odoo-10过滤树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在res.user中添加了"location_id"字段,以将用户连接到某个位置

i added 'location_id' field to res.user to connect user to a certain location

类ResUser(models.Model): _inherit ='res.users'

class ResUser(models.Model): _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的访问

i wanted to filter only the visits with 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\":[]}"}}

在视图中唯一可以直接调用的字段是uid lang ...等 所以我试图重写read方法来更新上下文,但是它似乎没有在视图中更新,仍然给出了未定义的'current_location_id'

the only fields available in view to be called directly are uid lang ... etc so i tried to override read method to update context, but it doesn't seem to be updated in the view,still gives undefined '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)

visit_tree_view

visit_tree_view

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

访问动作

<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过滤了视图,但在上下文中正确设置了该视图 战车

the default_search_current_id is working fine ,but the filter filter_my_visits does filter the view with current_location_id=none while its correctly set in context tanks

推荐答案

应该可以通过一些解决方法来实现.代替使用ir.actions.act_window尝试使用ir.actions.server,它应该返回窗口动作. 为什么?因为您需要将用户的位置ID放入该窗口操作的上下文中的可能性.我只能通过对代码使用服务器操作来看到这种可能性.

It should be possible with a little workaround. Instead of using a ir.actions.act_window try to use a ir.actions.server which should return a window action. Why? Because you need a possibility to get the user's location ID into the context of that window action. I only see this possibility by using a server action with code.

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

Just fill up the context for the window action with the location 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')]"/>

这篇关于如何使用动态字段odoo-10过滤树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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