This question has been flagged
4 Replies
2826 Views

Hi,

I need help to make one2many field be readonly when button clicked.
this my code ;

<form string="Sesi Parkir">
	<header>
		<button name="action_close" type="object" string="Sesi Berakhir" states="in" class="btn-primary" />
		<field name="state" widget="statusbar" statusbar_visible="in,out"/>
	</header>

parking_log_ids=fields.One2many('parking_log','parking_log_id',string='Log Parkir')
@api.multi def action_close(self): self.write({'state': 'out'}) self.write({'end_time':fields.Datetime.now(self)}) self.write({'parking_log_ids':'readonly=True'}) # i try like this but nothing happen :v

when I click the 'action_close' button, the one2many field becomes readonly and cannot add the record again

Thanks before :D
Avatar
Discard
Best Answer

Hi Fazryan,

Why don't you simply add a domain in the XML view depending on the state for example? I see you move the state to 'out' and fill in an 'end_time' so you can do it depending on that:

<field name="parking_log_ids" attrs="{'readonly': ['&amp;', ('state', '=', 'out'), ('end_time', '!=', False)]}"/>

Regards,
Yenthe

Avatar
Discard
Author

Hello Yenthe, thanks for response.

i implementation your code but still can 'add item'

even though the session status has ended

see this image ; https://drive.google.com/file/d/163ZqYsFbosgB0tgZ3Ve-Qdokd4kmSqoP/view?usp=sharing

Author

the point is i want is when status has ended, one2many field cannot 'add an item'

Best Answer

hello,

your py code something like 

@api.multi
def action_close(self):
self.write({'state': 'out', 'end_time':fields.Datetime.now()})

into xml add code like

<field name="parking_log_ids" attrs="{'readonly': [('state', '=', 'out')]}">   
 <tree>
        <field name="your_fieldname"/>
  </tree>
</field>
Avatar
Discard