程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django obtains the previous and next object information according to the query object

編輯:Python

If the database is self increasing ID, It's easy to get the previous and next records :

Obj = models.Article.objects.get(id=id)

prev = models.Article.objects.get(id = id-1)

next = models.Article.objects.get(id = id+1)

 

But I was When building data tables , Forget to add auto increment ID 了 , We had to find another way .

At present, we have found a very wonderful way , Share with you :

Install first django-next-prev package :

pip install django-next-prev

Then introduce the :

from next_prev import next_in_order,prev_in_order

 

obj = models.Article.objects.get(...)

next = next_in_order(obj)

prev = prev_in_order(obj)

 

You can get the list from other fields , Then get the previous and next data :

objs = modes.Article.objects.filter(list=abcde)

obj = objs.get(...)

next = next_in_order(obj,qs=objs)

prev = prev_in_order(obj,qs=objs)

Successfully obtain the previous and next objects .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved