Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
7426 Lượt xem

Hello,

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!

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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
Ảnh đại diện
Huỷ bỏ
Tác giả

You are a genius! Thanks!

Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 5 22
10465
1
thg 3 15
7579
6
thg 6 17
42134
2
thg 6 16
3204
3
thg 1 16
4527