如何在 Django Rest Framework 中对同一端点的不同 objects 进行分页?
How to paginate different objects of the same endpoint in Django Rest Framework?
以免假设我有一个名为 Collection
的模型。我可以创建一个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
这个端点需要 return 类似的东西:
{
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 分页。我喜欢性能,所以我应该为每种类型的 collection 创建一个特定的端点吗?但是,每次用户加载 collections 页面时,每个页面都会显示 12(每页最多)collections(my_collections、共享等),然后他就可以分页了它。我不知道这是否是最好的方法,我想很多用户每次加载页面时都会发送 3 个请求。
另一种方法:使用一个端点来加载初始页面,这个端点将向第一页发出一个请求,并且将使用不同的端点进行分页。
我真的不知道是否有更好的方法或类似的方法。
通常的方法是创建三个不同的端点。当用户向 api 发送 3 个请求时没问题,实际上它可能更好,因为您的前端可以更动态地显示数据。此外,您不需要每次都 return 所有数据,只需一个用户请求的列表。
如果您使用 ModelViewSet,因为它是三个端点的一个模型,您只需使用 @action
装饰器添加新操作并覆盖 get_queryset
。所以你会有这样的端点 - collections/shared_with_company
、collections/shared_list
和 collections/my_collections
并且在您的 get_queryset 中,您可以确定您想要 return 的查询集,例如:
def get_queryset(self):
queryset = super().get_queryset()
if self.action == 'list':
return queryset
elif self.action == 'shared_with_company':
return queryset.filter(share_with_company=True)
以免假设我有一个名为 Collection
的模型。我可以创建一个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
这个端点需要 return 类似的东西:
{
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 分页。我喜欢性能,所以我应该为每种类型的 collection 创建一个特定的端点吗?但是,每次用户加载 collections 页面时,每个页面都会显示 12(每页最多)collections(my_collections、共享等),然后他就可以分页了它。我不知道这是否是最好的方法,我想很多用户每次加载页面时都会发送 3 个请求。
另一种方法:使用一个端点来加载初始页面,这个端点将向第一页发出一个请求,并且将使用不同的端点进行分页。
我真的不知道是否有更好的方法或类似的方法。
通常的方法是创建三个不同的端点。当用户向 api 发送 3 个请求时没问题,实际上它可能更好,因为您的前端可以更动态地显示数据。此外,您不需要每次都 return 所有数据,只需一个用户请求的列表。
如果您使用 ModelViewSet,因为它是三个端点的一个模型,您只需使用 @action
装饰器添加新操作并覆盖 get_queryset
。所以你会有这样的端点 - collections/shared_with_company
、collections/shared_list
和 collections/my_collections
并且在您的 get_queryset 中,您可以确定您想要 return 的查询集,例如:
def get_queryset(self):
queryset = super().get_queryset()
if self.action == 'list':
return queryset
elif self.action == 'shared_with_company':
return queryset.filter(share_with_company=True)