Skip to Content
Menu
This question has been flagged
1 Reply
6199 Views

I have created a new model called project.phase, referenced on the project.project model from a one2many field (x_project_phaselist).

project.phase has the following fields: name, start_date, end_date, total_days, project_id

Working good. Now I need to get the overall for those phases to get the project totals.

I have managed to get the sum of total_days, using the following code:

for record in self:
    record['x_project_total_phases'] = sum(line['total_days'] for line in record['x_project_phaselist'])

Now I need to get the lowest start_date and the highest end_date, to be stored on two new fields on project.project. With the following code, I've been able to get the lowest date but from all the project.phase on all the projects, not the ones referenced on the actual project, referenced by project_id

for record in self:
recs = record['x_project_phaselist'].search([], order='start_date asc')          
      if recs:           
        record['x_project_min_date'] = recs[0].start_date

I need help to get only the phases for that project to be taken on account. Also I need help getting the higher end_date, as using the same but changing start_date for end_date and changing the asc for desc is not doing anything.

For info, I'm using the inbuilt editor, not coding new modules on .py but using the web builder, which I think is a very nice tool.


Avatar
Discard
Author Best Answer

Auto answering my question, just in case it can help someone. I fixed it with the following code:

for record in self:
dates = [] #Declaring empty array
    for line in record['x_project_phaselist']: #foreach phase
       if isinstance(line.end_date, datetime.date): #if the date is set
          dates.append(line.end_date) #Add it to the array
    record['x_project_max_date'] = max(dates) #Get the max from that array

I need to create the array and check for the type so I don't get the unordenable error when empty dates are taken.

It's working for now.


Avatar
Discard
Related Posts Replies Views Activity
2
Nov 22
4363
1
Jun 22
6765
1
Sep 21
2040
0
Aug 21
95
0
Jul 21
172