Skip to content

Limiting Queryset #1355

Answered by saadmk11
ByteOven asked this question in Q&A
Mar 30, 2021 · 2 comments · 3 replies
Discussion options

You must be logged in to vote

If you are using FilterView, BaseFilterView, or any similar approach you can use limit after the filtering is done by django-filter.
For Example,

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…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@ByteOven
Comment options

Comment options

You must be logged in to vote
2 replies
@carltongibson
Comment options

@saadmk11
Comment options

Answer selected by carltongibson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants