【发布时间】:2022-01-26 18:10:14
【问题描述】:
以免我有一个名为Collection 的模型。我可以创建一个collection,这个集合有两个重要的字段:share_with_company,share_list。
class Collection(models.Model):
name = models.CharField(max_length=200, blank=False, null=False)
share_with_company = models.BooleanField(default=False)
share_list = ArrayField(models.CharField(max_length=15), null=True, blank=True)
owner = models.CharField(max_length=200, blank=False, null=False)
目前我有一个端点:/collections
这个端点需要返回类似的东西:
{
shared_with_company:{collections: ..., count: 5}
shared_list:{collections: ..., count: 12}
my_collections:{collections: ..., count: 20} //dont shared with anyone, created by the current user
}
但是,在前端,用户只想对 my_collections 或 shared_list 或 shared_with_company 进行分页。我喜欢性能,所以我应该为每种类型的集合创建一个具体的端点吗?但是,每次用户加载集合页面时,都会显示每个集合(my_collections、shared 等)的 12 个(每页最大)集合,然后他将能够对其进行分页。我不知道这是否是最好的方法,我认为很多用户每次加载页面时都会发送 3 个请求。
另一种方法:使用端点加载初始页面,该端点将向第一页发出一个请求,并使用不同的端点进行分页。
我真的不知道是否有更好的方法或类似的方法。
【问题讨论】:
-
你的 ViewSet 看起来怎么样?我的意思是此时您已经创建了端点,对吗?在您的问题中包含相关代码。
标签: django rest django-rest-framework