Skip to Content
Menu
This question has been flagged

HI All,

Here I am in a situation to display either a charecter or integer field which is capable to store a 10 digit value,

and need to display it in semi password/ semi visible format.

I mean if the account number is 9876543210123, i just want to show 987*******123 in the list or tree view.

Can anyone help me to do the above formatting, Is there any default widget available.

Also any suggestions for creating a custom widget or any other way to acheive the above need is appeciable.

Thanks In Advance.

Avatar
Discard
Best Answer

Komala Kiran Kumar,

If you want the whole text as *, use widget="password", or password='1'.

You can override the read method which can do the job better.

See the example:

vals = super(account_voucher, self).read(cr, uid, ids, fields, context, load)
        for val in vals:
            if val.get('cc_number', False) :
                dec_data = rsa_encrypt.decrypt(val.get('cc_number', False), val.get('key', False))
                val['cc_number'] = dec_data
                if context.get('cc_no', '') != 'no_mask':
                    i = len(val['cc_number']) - 4
                    val['cc_number'] = 'X' * i + val['cc_number'][-4:len(val['cc_number'])]
        if not is_list:
            vals = vals[0]
        return vals

Thanks.

Avatar
Discard
Author

Hi, This has really helped me, but i am here with a small problem where as in form view also it is showing the same, but i just want to show that in Tree or List view

Dear Friend, you may do this based on context. Set context on the xml definition and check!

Best Answer

Hello Komala,

Based of the answer of Serpent CS, you can also create a new char field (fields.function), which will store the formatted value based on the original one. With this, you could use your original field in the form view, and only display the new computed field in your list.

The solution of Serpent CS (using the context to customize the value returned by read() ) is a bit simple than mine to implement, but i prefer not to override the read() method for each customization (this is more difficult to maintain and less optimized than a dedicated field) ^^

Finally, it is easier to define the groups access when 2 different fields are defined (one for users having 'full' access to sensitve data, the other for 'restricted' access users).

For this function field, you can make it with the parameter store to True (as the original value is on the same model), and use the method posted by Serpent CS to compute the value. For example : 

'new_field': fields.function(_get_formatted_account, type='char', method=True, store=True)  # get_formatted_account is the method to define with the code of Serpent CS

# for optimiaztion: store={'your.model.name': (lambda self, cr, uid, ids, ctx: ids, ['your_account_field'], 10)}

Hope it helps you,

Regards

Avatar
Discard
Related Posts Replies Views Activity
2
Mar 15
7719
1
Nov 19
3498
9
Jun 15
12906
0
Jan 25
1
0
Dec 23
1361