Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
6 Risposte
15249 Visualizzazioni

i want to view or hide 'working_hours' field based on function ( Automatically without using button or anything)

@api.onchange('working_hours_view')
def hide_working_hours(self):
if self.env.uid == self.user_id.id or self.env.uid == self.parent_id.user_id.id or self.env.user.has_group(
'hr.group_hr_user'):
self.working_hours_view = True
else:
self.working_hours_view = False

working_hours_view = fields.Boolean(computed=hide_working_hours)
this is view:
<record id="hide_working_hours_for_employees" model="ir.ui.view">
<field name="name">Hide Working Hours Employees Form</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='resource_calendar_id']" position="before">
<field name="working_hours_view" invisible="1"/>
</xpath>
<xpath expr="//field[@name='resource_calendar_id']" position="attributes">
<attribute name="attrs">{'invisible': [('working_hours_view' ,'=', False)]}</attribute>
</xpath>
      </field>
</record>
Avatar
Abbandona

How to visible and invisible fields in odoo: https://goo.gl/BCxCpk

Risposta migliore

Your code seems correct. Just remove "api.onchange" decorator from your method and try it again.
System will automatically trigger the function without depending on any of the field.

Avatar
Abbandona
Autore

i deleted "api.onchange" decorator but still not working

Risposta migliore

  <field name="working_hours"  attrs="{'invisible': [('field name ', '=', False)]}"/>

You can do this by using attrs field invisible now you just need to set condition field_name = False or field_name != False . Like this that working hours field will be hidden or will be displayed on based on condition.

Thanks. 

Avatar
Abbandona
Risposta migliore

You Can hide a field or Button using below code , its work for me  and i'm using odoo version 9

here i'm hidding a button using which i'm checking a field(work_email) value is emty or not

my py file ( here i'm using inherited view of hr employee )

work_email = fields.Char('Work Email')
hide = fields.Boolean(string='Hide', compute="_compute_hide" ,store=False)

@api.depends('work_email')

def _compute_hide(self):

if not self.work_email:

self.hide = True

else:

self.hide = False

my xml file

<button class="oe_stat_button" name="send_personal_mail" string="Send Email"  type="object" icon="fa-envelope" attrs="{'invisible': [('hide','=', True)]}" /> 

<field name="hide" invisible="1" />

Avatar
Abbandona
Autore Risposta migliore

i added the field before the function without depends or onchange and it works now automatically .. like this example:


class InheritHrEmployee(models.Model):
_inherit = 'hr.employee'

inv = fields.Boolean(string="Invisible", compute="c_inv", store=False)

@api.one
def c_inv(self):
if self.env.uid == self.user_id.id or self.env.uid == self.parent_id.user_id.id or self.env.user.has_group(
'hr.group_hr_user'):
self.inv = False
else:
self.inv = True



Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
0
feb 20
2847
2
gen 20
7287
2
dic 19
5501
0
gen 25
1227
1
mar 25
652