Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


How Can I get the Previous Record in a Table?

on 12/3/13, 11:44 PM 1,178 views


Sorry if this is super basic, I am almost new with python.

So, basically I am passing an ID to a function and I need to get the record just before the one with the ID I am passing from a table.

This is my function

def get_prev_period(self,period_id):
        if period_id:
            period_list = self.pool.get('sim.periods').browse(self.cr, self.uid, period_id)
            current_id = period_list.id
            #Now I need to get the ID just above current_id
        return 0

So, basically I need to get the ID just above the current_id. I am not able to do current_id - 1 because that might no be the case.

Any tips will be much appreciated! Thanks!

Dhana Babu
On 12/4/13, 1:19 AM

You need to execute your manual query on postgres database table,

def get_prev_period(self,period_id):
            if period_id:
               result= self.cr("""SELECT id FROM table_name WHERE id < %s ORDER BY id DESC LIMIT 1""", [period_id])
               result = self.cr.dictfetchall()
               print result # holds the one row data
               current_id = result[0]['id']
            return 0

You are a genius! Thanks!

on 12/4/13, 9:00 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)


Asked: 12/3/13, 11:44 PM
Seen: 1178 times
Last updated: 3/16/15, 8:10 AM