How to customize the Queryset in the Django admin change list view?
Customize the Queryset in the Django admin change list view.
Play this article
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.