Skip to Content
Menu
This question has been flagged
9 Replies
9313 Views

Py File

class DailyLog(models.Model):

_name = "daily.log"

_inherit = "mail.thread"

_order = "id desc"

@api.multi

def _current_user_get_fnc(self):

self.current_user = self.env.user.id

        user_id = fields.Many2one('res.users', 'Assign to', track_visibility='onchange', default=lambda self: self.env.user, index=True)

current_user = fields.Many2one('res.users', 'Current User', compute='_current_user_get_fnc', store=False)

View file

<record id="daily_log_form_view" model="ir.ui.view">

<field name="name">daily.log.form</field>

<field name="model">daily.log</field>

<field name="arch" type="xml">

<form string="Daily Log">

<sheet>

                        <group>

                            <group>

<field name="user_id" options='{"no_create": True}' context="{'user_preference': 1}"/>

</group>

<group>

<field name="current_user"/>

</group>

</group>

</sheet>

</form>

</field>

</record>

<record id="daily_log_form_view_inherited" model="ir.ui.view">

<field name="name">daily.log.form.inherited</field>

<field name="model">daily.log</field>

<field name="inherit_id" ref="crm_debranding.daily_log_form_view"/>

<field name="groups_id" eval="[(6,0, [ref('crm_base_debranding.group_telesales')])]" />

<field name="arch" type="xml">

<xpath expr="//form/sheet//field[@name='name']" position="attributes">

<attribute name="attrs">{'readonly': [('user_id', '!=', create_uid)]}</attribute>

</xpath>

</field>

</record>

Avatar
Discard

The best method would be using the invisible field which holds the user id, and in attrs you can easily compare them

Author

Thanks Hilar for response. It will not work, i tried that too.

Best Answer

Hi,

In the XML you will get the current logged in the user id from user.id so just replace the current_user from xml with user.id   ( for giving domain)

('user_id', '=', user.id)

Uptd :

Add a new boolean field(compute), then compare the two fields from the python , and either set true or false to this field and based on this field make the field readonly

Thanks

Avatar
Discard
Author

Thanks Niyas for response. We cannot use user.id in attrs, It can be used in domains.

oops, didn't noted the attrs along with the question

Author

Its ok.

Add a new boolean field(compute), then compare the two fields from the python , and either set true or false to this field and based on this field make the field readonly

Author

Thanks Niyas for alternate solution, it seems good if no other alternate.

Best Answer

Try this.

<field name="other_field" invisible="1"/>
<field name="campus_id" attrs="{'readonly':[('other_field','=',False)]}"/>

In above code we have created a computed boolean field and on the basis of this field we are going to make readonly "campus_id" field.


Avatar
Discard
Related Posts Replies Views Activity
0
Sep 23
497
0
Sep 23
450
2
Feb 23
9253
6
Oct 23
19273
3
Mar 24
7688