Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
6 Respostas
15146 Visualizações

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
Cancelar

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

Melhor resposta

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
Cancelar
Autor

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

Melhor resposta

  <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
Cancelar
Melhor resposta

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
Cancelar
Autor Melhor resposta

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
Cancelar
Publicações relacionadas Respostas Visualizações Atividade
0
fev. 20
2807
2
jan. 20
7212
2
dez. 19
5465
0
jan. 25
1181
1
mar. 25
652