Skip to Content
Menu
This question has been flagged
1 Reply
8183 Views
Hi, I need a help :

if date_fin == date.today :

I want the cycle statut change (activer to desactiver) and all the rest of the fields to be in readonly mode.


class ProductionCycle(models.Model):
  _name = 'production.cycle'
  _description ="Les cycle de production"
  _inherit = ['mail.thread']

  name = fields.Char(string='Nom du cycle de production', required=True, track_visibility='always')
  description = fields.Text(string='Description', track_visibility='onchange')
  date_debut = fields.Date(string='Date de début', default=fields.Date.context_today, track_visibility='onchange')
  date_fin = fields.Date(string='Date de fin', required=True, track_visibility='onchange')
  statut = fields.Selection([('activer', 'Activer'), ('desactiver', 'Desactiver')], string='Statut', default='activer', required=True, track_visibility='onchange')
  batiment_id = fields.Many2one('technique.batiment', string='Bâtiment', ondelete='cascade', required=True, track_visibility='onchange')
  souche_id = fields.Many2one('animale.souche', string='Souche', ondelete='cascade', required=True, track_visibility='onchange')
 
  _sql_constraints = [
('name_uniq', 'unique (name)', "Le nom du cycle existe déjà !"),
]


Thanks

Avatar
Discard
Best Answer

Hi Ramla,

To create an onchange function, in your <models>.py file add

@api.onchange('date_fin')
def _method_name(self):    
    if self.date_fin:
        if self.date_fin == date.today():    #assuming that date is a datetime object
            self.statut = 'desactiver' 

And to make the fields set to readonly in your xml file, i suggest you use 

<field name="field_name" attrs="{'readonly':[('statut', '=', 'desactiver')]} />

Just work around on this and I'm sure you'll get what you want.. 

Hope it helps. :)

Avatar
Discard
Author

Thank you so much ^_^

Related Posts Replies Views Activity
0
Apr 19
2867
4
May 19
3805
2
May 19
2835
2
Apr 19
3349
1
Apr 19
4187