This question has been flagged
6 Replies
7184 Views

I want to get the latest date from a date field and show it in a new field of an other view so I used this function and it seems I'm doing something wrong what could it be?here is my code

def _get_latest_intervention(self, cr, uid, ids, latest, args, context=None):
       res = {}
       obj_intervention = self.pool.get('c.intervention'
       )for interv in self.browse(cr, uid, ids, context=context):
           intervention_ids = obj_intervention.search(cr, uid,    [('equipement_id' '=',, intervention_id)], order='date_intv'
           , context=context)if intervention_ids:
               res[intervention_id] = intervention_ids[-1:][0
           ]else False
       return:
               res[intervention_id] = res

to explain more this module is about management of technical centers of vehicul so I want to show the latest date of intervention of those technical centers in their form view in the field "latest" so I have created 3 menu items (centre,equipement,intervention) and I have put this function in the centre class

Avatar
Discard

You are looping (for _ in self.browse...) in elements of class intervention? I don't know, I'm just guessing from variable names. Is this method inside the class "Centre" ? What's the field that links togheter "centre" and "intervention"? What does equipement has to do with all this?

Author

yes this methode is in the class"centre" ,I did this fault I wanted to wright 'centre_id' but I wrote 'equipement_id' so there is no equipements menchened in this function every centre has his equipements and all dates of intervention of equipments either it's a corrective maintenace or a preventive one so I want to add a new field in the centre form view named latest,which shows the latest added date in the intervention form view this classes are related of course with relational fields I will put all code to make it clearer

Author Best Answer

I changed somethings in my code so I want to show the date in the intervention_id field, here is my code 

# -*- coding: utf-8 -*-

from openerp import models, fields, api

class centre(models.Model):

_name = 'res.partner'

_inherit = ['res.partner']

def _get_latest_intervention(self, cr, uid, ids, field_name, args, context=None):

res = {}

obj_intervention = self.pool.get('c.intervention')

for interv in self.browse(cr, uid, ids, context=context):

intervention_ids = obj_intervention.search(cr, uid, [('centre_id', '=', intervention_id)], order='date_intv', context=context)

if intervention_ids:

res[intervention_id] = intervention_ids[-1:][0]

else:

res[intervention_id] = False

return res

rs_ctr = fields.Char(string='Réseau')

nb_ligne = fields.Integer(string='Lignes')

n_agr = fields.Integer(string='N° d\'agrèment')

chef = fields.Char(string='Chef centre')

prp = fields.Char(string='Propriétaire')

equipement_id = fields.Many2one('product.template','Equipements',select=True)

intervention_id = fields.Date(compute='_get_latest_intervention', type="Date", string='Derniére intervention')

  

properties1 = fields.One2many('product.template','centre_id','Centres')

centre()

class equipement(models.Model):

_name = 'product.template'

_inherit = ['product.template']

name = fields.Char(string='Nom')

num_ligne = fields.Integer(string='N° ligne')

model_mat = fields.Char(string='Model de materiel')

centre_id = fields.Many2one('res.partner','Centres',select=True)

properties2 = fields.One2many('c.maintenance','equipement_id','Equipements')

equipement()


class maintenance(models.Model):

_name = 'c.maintenance'

_description = 'Maintenance'

STATE_SELECTION = [('c','Corrective'),('p','Préventive')]

type_int = fields.Selection(STATE_SELECTION,'Type d\'intervention')

date_intv = fields.Date(string='Date d\'intervention')

equipement_id = fields.Many2one('product.template','Equipements',select=True)

intervention_id = fields.Many2one('c.intervention','Interventions',select=True)

maintenance()

class intervention(models.Model):

_name = 'c.intervention'

STATE_SELECTION = [('c','Corrective'),('p','Préventive')]

_description = 'Interventions'

_inherits = {'res.partner':'centre_id'}

_inherit = {'c.maintenance'}

name = fields.Char(string='Nom')

type_int = fields.Selection(STATE_SELECTION,'Type d\'intervention')

properties4 = fields.One2many('c.maintenance','intervention_id','Interventions')

centre_id = fields.Many2one('res.partner','Centres',select=True)

intervention()

what could be the right way to make this work please 

Avatar
Discard

Is there anyway to record the intervention of a field in another field? like computing the field to store the intervention date of another field?

Best Answer

Hi,

Did you define intervention_id as a function field in another object in which you want to see latest intervention ?

Avatar
Discard
Author

no I definded field latest as the function field am I doing something wrong?