Skip to Content
Menu
This question has been flagged
4 Replies
7712 Views

Hi, 

i'm trying to fetch the second last record of a model ( the record before the last) ,

here what i did but it does not work.

second_last = self. env['project.task'].search([()], order='id desc', limit=2)

How to get it please ?

Thanks 

Avatar
Discard

Hi, Your code should return with two records having largest id. Can you specify what is your current output and what are you expecting.

Author

@Ray Carnes

Thanks for your answer,

I added two one2many tree in "project.task" wich contains fields, also i added new model "task.situation" wich also contains one2many tree. Now in "project.task" form i added button to create new situation in "task.situation". what i want is, that there are field in the one2many tree that in "task.situation" model should take 0 for the first situation created for that "project.task_id" else it takes the value of another field from the o2m tree of the last situation created of that "project.task".

I asked here about how to get the value of the second last record because actually the one2many tree in "task.situation" have to be filled from the two different one2many trees from "project.task". what i tried to do is while the creation of new situation from "project.task" i create this situation with values of the first o2m tree of "project.task" and that i made test on the second o2m tree in "project.task" if it contains lines i update the situation already created to add into it the new lines. that is why i needed to get the second last record of the situation, because i said above i have a field in one2many tree always depends on the value of another field but of last situation.

I whould be pleased if you give me your suggestion

Thanks

Best Answer

You should get it by this way,

second_last = self. env['project.task'].search([()], order='id desc', limit=2)[1]

If there won't be enough records the second_last will return False. Otherwise you will get second last browsable record and if you want ID not record then just put [1].id at last.


This is the way how you can achieve it, but you should share you requirement what you want to achieve, so that you will get better response from the community.

 


    

Avatar
Discard
Best Answer

The ID field is not guaranteed to be without gaps.  What happens if someone deletes a project task?

What is the business problem you are trying to solve?

Avatar
Discard