6.6 Pagination對于資源集合,分頁獲取是一種比較合理的方式。如果基于開發(fā)框架(如Django REST Framework),直接使用開發(fā)框架中的分頁機(jī)制即可,如果是自己實(shí)現(xiàn)分頁機(jī)制,Gevin的策略是:返回資源集合是,包含與分頁有關(guān)的數(shù)據(jù)如下:{ ?"page": 1, ? ? ? ? ? ?# 當(dāng)前是第幾頁 ?"pages": 3, ? ? ? ? ? # 總共多少頁 ?"per_page": 10, ? ? ? # 每頁多少數(shù)據(jù) ?"has_next": true, ? ? # 是否有下一頁數(shù)據(jù) ?"has_prev": false, ? ?# 是否有前一頁數(shù)據(jù) ?"total": 27 ? ? ? ? ? # 總共多少數(shù)據(jù)}當(dāng)想API請求資源集合時(shí),可選的分頁參數(shù)為:參數(shù)含義page ? ?當(dāng)前是第幾頁,默認(rèn)為1 ? ?per_page ? ?每頁多少條記錄,默認(rèn)為系統(tǒng)默認(rèn)值 ? ?另外,系統(tǒng)內(nèi)還設(shè)置一個(gè)per_page_max字段,用于標(biāo)記系統(tǒng)允許的每頁最大記錄數(shù),當(dāng)per_page值大于?per_page_max?值時(shí),每頁記錄條數(shù)為?per_page_max。https://blog.igevin.info/posts/restful-api-get-started-to-write/
添加回答
舉報(bào)
0/150
提交
取消