Skip to main content

Command Palette

Search for a command to run...

How to customize the Queryset in the Django admin change list view?

Customize the Queryset in the Django admin change list view.

Updated
1 min read
How to customize the Queryset in the Django admin change list view?
G

Experienced Full Stack Software Engineer with 4+ Years of experience, proficient in Django and React, with expertise in Python, JavaScript and Typescript, also an author of Dev.Junction Blogs & YouTube Channel, expert in Python/Django, Rest Framework, ReactJS/NextJS and TypeScript, currently based in Pune, India.

Note: If you want to read the blog series on Django Admin customization, then click here.

Suppose you have a Book model as given below.

# app_name/models.py

class Book(models.Model):
    ...
    title = models.CharField(max_length=150)
    is_active = models.BooleanField(default=True)

And you just want to show the model which has is_active=True in the list view, here is how you do it.

# app_name/admin.py

from django.contrib import admin
from django.db import models
from app_name.models import Book

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    ...
    def get_queryset(self, request):
        queryset = super().get_queryset(request)
        new_queryset = queryset.filter(is_active=True)
        return new_queryset

Any thoughts? Write it down in the comments.

For more such crispy blogs daily, follow Dev.Junction, subscribe to our newsletter and get notified.

R

mindblowing

Django admin tips and tricks

Part 4 of 14

Django provides a great Admin panel rich in features and highly customizable, in this series of blogs we will be introduced to all such possible customizations and more.

Up next

How to change the input widget into a textfield in Django Model Admin form?

Change the input widget into a textfield in Django Model Admin form