python
26 lines · 7 steps
Building a scoped, filtered ListView in Django
A class-based ListView that restricts invoices to the current user, eager-loads relations, and applies optional status filtering.
Explained by
highlit
1from django.contrib.auth.mixins import LoginRequiredMixin
2from django.views.generic import ListView
3
4from .models import Invoice
5
6
7class InvoiceListView(LoginRequiredMixin, ListView):
8 model = Invoice
9 template_name = "invoices/invoice_list.html"
10 context_object_name = "invoices"
11 paginate_by = 25
12
13 def get_queryset(self):
14 queryset = (
15 super()
16 .get_queryset()
17 .filter(owner=self.request.user)
18 .select_related("client")
19 .order_by("-issued_at")
20 )
21
22 status = self.request.GET.get("status")
23 if status in {"draft", "sent", "paid", "overdue"}:
24 queryset = queryset.filter(status=status)
25
26 return queryset
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Overriding get_queryset lets you scope and shape the data a generic view exposes.
- 2Chaining select_related avoids N+1 queries by eager-loading related rows.
- 3Validating query params against a known set keeps user input from reaching the ORM unchecked.
Related explainers
python
from flask import Blueprint, jsonify, request, abort v1 = Blueprint("users_v1", __name__) v2 = Blueprint("users_v2", __name__)
Versioning a Flask API with Blueprints
api versioning
blueprints
rest
Intermediate
7 steps
typescript
import { Injectable, UnauthorizedException } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { ConfigService } from '@nestjs/config';
How a JWT strategy authenticates in NestJS
authentication
jwt
passport
Intermediate
7 steps
python
import time import threading from enum import Enum from functools import wraps
Building a circuit breaker in Python
circuit-breaker
resilience
decorators
Advanced
7 steps
typescript
import { Injectable } from '@angular/core'; import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from '@angular/common/http'; import { BehaviorSubject, Observable, throwError } from 'rxjs'; import { catchError, filter, take, switchMap } from 'rxjs/operators';
Refreshing auth tokens in an Angular interceptor
http-interceptor
token-refresh
rxjs
Advanced
8 steps
go
package auth import ( "net/http"
Setting and reading secure session cookies in Go
cookies
session-management
security
Intermediate
6 steps
python
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector from django.core.cache import cache from django.db.models import F from django.http import JsonResponse
Ranked full-text search in Django
full-text-search
debouncing
caching
Advanced
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-scoped-filtered-listview-in-django-explained-python-a4f7/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.