Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
7 Trả lời
20864 Lượt xem

Basic idea: Hide few fields in client form view for OpenERP users which are not defined as VIP users in my custom many2many table (user_id, partner_id)

What I've already done:

In res.partner form view:

<field name="vip_ids" widget="many2many_tags" placeholder="VIP users..."/>

My custom module which adds many2many relationship for res.partner module:

from openerp.osv import orm, fields
from osv import fields, osv
class res_partner_users_vip_rel2(osv.osv):
    _inherit = 'res.partner'
    _description = "VIP status for partner (users)"
    _columns = {
        'vip_ids': fields.many2many(
            'res.users',
            'res_partner_users_vip_rel2',
            'partner_id',
            'users_id',
            'VIP status'),
    }
res_partner_users_vip_rel2()

In Form View I can successfully save multiple users in newly created field, but I am stuck at hiding other fields for users who are not defined in my many2many relationship.

As I understand, I need to use attrs attribute but I don't understand how to use it with many2many relationship.

This doesn't work:

<field name="mobile" attrs="{'invisible': [('uid','in', vip_ids)]}"/>
<field name="fax" attrs="{'invisible': [('uid','in', vip_ids)]}"/>
<field name="email" widget="email" attrs="{'invisible': [('uid','in', vip_ids)]}"/>

Any advice would be greatly appreciated :)

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

I have try this and working :

       <record id="view_0001" model="ir.ui.view">  
            <field name="name">XXXXXXX</field>
            <field name="model">res.partner</field>
            <field name="arch" type="xml">
                <form string="XXXXXXXXXX" version="7.0">
                    <group col="4" colspan="2">
                        <field name="ids_count"/> 
                        <field name="mobile" attrs="{'invisible': [('ids_count','=', 0)]}"/>
                        <field name="fax" attrs="{'invisible': [('ids_count','=', 0)]}"/>
                        <field name="email" widget="email" attrs="{'invisible': [('ids_count','=',0)]}"/> 
                    </group>
                    <field name="vip_ids"></field>

                </form> 
            </field>
       </record>

with modified model definiton

from openerp.osv import fields, osv
class res_partner_users_vip_rel2(osv.osv):
    _inherit = 'res.partner'
    _description = "VIP status for partner (users)"
    def getcount(self, cr, uid,ids,name,arg,context):
        res={}
        sql="""
            SELECT partner_id id, count(*) cnt FROM res_partner_users_vip_rel2 
            WHERE partner_id = """+str(ids[0])+""" GROUP BY partner_id """
        cr.execute(sql)
        res.update(dict(cr.fetchall()))
        if res!={}:
            return res 
        return {ids[0]:0}
    _columns = {                
        'ids_count':fields.function(getcount,type="integer",string='Count'),

        'vip_ids': fields.many2many(
            'res.users',
            'res_partner_users_vip_rel2',
            'partner_id',
            'users_id',
            'VIP status'),
         }

Ảnh đại diện
Huỷ bỏ
Tác giả

Thanks, but doesn't seem to work. Field is shown both when user is added in many2many relation and also when he s not

I have edited my previous answer, and i have tried. It work.

Tác giả

thank You! Only I added AND users_id= """+str(uid)+""" to the query

Câu trả lời hay nhất

You can also just compare to an empty array (I'm using Odoo 11)

<div attrs="{'invisible':[ ('boats', '=', []) ]}">
<label for="boats" class="o_ff_header"/>
<field name="boats"/>
</div>

Ảnh đại diện
Huỷ bỏ

This way works great for me in Odoo 11, thanks !!

Câu trả lời hay nhất

Hello, 

It could be achieved without function field and only with xml. You can use attrs attribute like

attrs="{'invisible': [('m2m_field', '=', [(6, False, [])])]}"attrs="{'invisible': [('m2m_field', '=', [(6, False, [])])]}" 
            

On webclient m2m field outputs like [(6, False, [list_of_record_ids])], when there is no record list becomes empty and it outputs like [(6, False, [])] . It can be used in xml condition.

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

Thanks aharoen. Yesteday evening I came up with similar solution:

def _get_active_ids(self, cr, uid, ids, field_name, arg, context=None):
      result = {}
      for clase in self.browse(cr, uid, ids, context=context):
         result[clase.id] = False
         for membre in clase.vip_ids:
            if membre.id == uid:
                result[clase.id] = True
      return result

class res_partner_users_vip_rel2(osv.osv):
    _inherit = 'res.partner'
    _description = "VIP2 status for partner (users)"
    _columns = {
        'vip_ids': fields.many2many(
            'res.users',
            'res_partner_users_vip_rel2',
            'partner_id',
            'users_id',
            'VIP2 status'),
        'is_current_user_vip': fields.function(
            _get_active_ids,
            type='boolean',
            string="Does current user have VIP access?"),
    }

res_partner_users_vip_rel2()
Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 1 24
15154
1
thg 8 24
7725
1
thg 9 23
2399
7
thg 6 20
6817
2
thg 7 17
3433