【发布时间】:2018-01-08 20:21:30
【问题描述】:
当我需要为 REST API 创建资源 URL 时,我总是怀疑您可以在下面看到。我想知道是否有人可以帮助我。
假设我有两个模型。
- 用户
- 发布
用户可以提交自己的帖子,也可以评论自己的帖子和其他帖子。
用户的主要资源 URL 是:
GET /users # Retrieve all users.
POST /users # Create a new user.
GET/DELETE/PUT /users/{user_id} # Get, remove and update an user.
Post 的主要资源 URL 为:
GET /posts # Retrieve all posts.
POST /posts # Create a new post.
GET/DELETE/PUT /posts/{post_id} # Get, remove and update a post.
我的问题出现在我想要的时候:
前 10 名提交者(过滤参数(外部链接、讨论、全部))。网址应为:
GET /users/top?type=ext
GET /users/top?type=disc
GET /users/top # for all
或者应该是:
GET /users?top=ext
GET /users?top=disc
GET /users?top=all
相同,但有帖子:
前 10 条评论帖子(过滤参数(外部链接、讨论、全部))。网址应为:
GET /posts/comments?type=ext
GET /posts/comments?type=disc
GET /posts/comments # for all
或者应该是:
GET /posts?top=ext
GET /posts?top=disc
GET /posts?top=all
以上任何一个选项对您有好处还是应该是另一种方式?
问候
【问题讨论】:
标签: django rest api django-rest-framework