0Pricing
Django Academy · Lesson

Overriding get_queryset and Templates

Customize data and template names per view.

Overriding get_queryset and Templates is a free Django Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Make Generic Views Yours

Generic views give sensible defaults, but real apps need tweaks. You override a few hooks to customize them. 🔧

What get_queryset Does

get_queryset decides which rows a view works with, so overriding it lets you filter or sort the results.

Filtering with get_queryset

Return a filtered queryset to show only the rows you want, like published posts.

class PostList(ListView):
    model = Post
    def get_queryset(self):
        return Post.objects.filter(published=True)

Reading the URL or User

Inside get_queryset you can read self.request, so you can scope results to the logged-in user.

def get_queryset(self):
    return Post.objects.filter(author=self.request.user)

Override template_name

Do not like the default template path? Set template_name to point at any template you choose.

class PostList(ListView):
    model = Post
    template_name = "blog/home.html"

Override the Context Variable

Pair template_name with context_object_name so your custom template reads with a friendly variable.

class PostList(ListView):
    template_name = "blog/home.html"
    context_object_name = "posts"

Add Extra Context

Override get_context_data to pass extra values, like a page title, alongside the objects.

def get_context_data(self, **kwargs):
    ctx = super().get_context_data(**kwargs)
    ctx["title"] = "Blog"
    return ctx

Always Call super First

In get_context_data, call super() first so you keep the objects the view already added, then add your own.

Customize the Suffix

Prefer model_browse over model_list? Set template_name_suffix to change just the suffix Django appends.

class PostList(ListView):
    template_name_suffix = "_browse"

Order the Results

Override get_queryset to sort rows, for example showing the newest posts first.

def get_queryset(self):
    return Post.objects.order_by("-created")

Small Hooks, Big Control

With these few overrides you bend generic views to almost any need while keeping their tidy structure. 💡

Quick Check

What is the purpose of overriding get_queryset?

Recap: Customizing Views

You shaped generic views with get_queryset, template_name, and get_context_data, customizing data and templates without losing their simplicity. 🎉

Frequently asked questions

Is the “Overriding get_queryset and Templates” lesson free?

Yes — the full text of “Overriding get_queryset and Templates” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “Overriding get_queryset and Templates”?

Customize data and template names per view. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Django Academy?

No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Overriding get_queryset and Templates” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Django Academy lesson?

Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. ListView and DetailView
  2. CreateView and UpdateView
  3. DeleteView and success_url
  4. Overriding get_queryset and Templates
← Back to Django Academy