Limiting Queryset
#1355
-
Was wondering is there a option to limit the queryset. When I try to add limit / offset, the QS gives an error : |
Beta Was this translation helpful? Give feedback.
Answered by
saadmk11
Jul 10, 2021
Replies: 2 comments 3 replies
-
Filter before limiting (with the slice) |
Beta Was this translation helpful? Give feedback.
1 reply
-
If you are using def get(self, request, *args, **kwargs):
filterset_class = self.get_filterset_class()
self.filterset = self.get_filterset(filterset_class)
if not self.filterset.is_bound or self.filterset.is_valid() or not self.get_strict():
self.object_list = self.filterset.qs
else:
self.object_list = self.filterset.queryset.none()
context = self.get_context_data(
filter=self.filterset,
object_list=self.object_list[:10] # <-------------- Limit Here
)
return self.render_to_response(context) Or you can override the def filter_queryset(self, queryset):
return super().filter_queryset(queryset)[:10] |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
carltongibson
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you are using
FilterView
,BaseFilterView
, or any similar approach you can use limit after the filtering is done bydjango-filter
.For Example,
Or…