Django Admin: How to Set a Default Filter Without Conflicting with User-Selected Filters?

I’m working with the Django Admin panel and need to set a default filter for a model’s changelist view. When the user opens the changelist page for the first time, I want it to automatically filter by a specific value (e.g., status=”A”). However, when a user selects a different filter (e.g., status=”B”), the default filter (status=”A”) should not be applied.

What I Have Tried:
I’ve overridden the changelist_view method in my ModelAdmin class to check if any status filter is present in the request. If not, I add the default filter (status=A). Here’s what my code looks like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from django.contrib import admin
from django.shortcuts import redirect
from django.urls import reverse
from .models import Person # Replace with your actual model
class PersonAdmin(admin.ModelAdmin):
list_display = ('name', 'status') # Adjust fields to match your model
list_filter = ('status',) # Adjust filters to match your model
def get_queryset(self, request):
# Get the original queryset without any default filtering
qs = super().get_queryset(request)
return qs
def changelist_view(self, request, extra_context=None):
# Check if any filters related to 'status' are already applied by looking at request.GET
if 'status' not in request.GET and 'status__exact' not in request.GET:
# Redirect to the same changelist URL with the default filter applied
query = request.GET.copy() # Make a mutable copy of GET parameters
query['status'] = 'A' # Set the default filter value for 'status' to 'A'
# Redirect to the changelist URL with the correct query string
return redirect(
f"{reverse('admin:Human_Resource_Management_person_changelist')}?{query.urlencode()}"
)
# Call the original changelist_view method
return super().changelist_view(request, extra_context=extra_context)
# Register your admin class with the associated model
admin.site.register(Person, PersonAdmin)
</code>
<code>from django.contrib import admin from django.shortcuts import redirect from django.urls import reverse from .models import Person # Replace with your actual model class PersonAdmin(admin.ModelAdmin): list_display = ('name', 'status') # Adjust fields to match your model list_filter = ('status',) # Adjust filters to match your model def get_queryset(self, request): # Get the original queryset without any default filtering qs = super().get_queryset(request) return qs def changelist_view(self, request, extra_context=None): # Check if any filters related to 'status' are already applied by looking at request.GET if 'status' not in request.GET and 'status__exact' not in request.GET: # Redirect to the same changelist URL with the default filter applied query = request.GET.copy() # Make a mutable copy of GET parameters query['status'] = 'A' # Set the default filter value for 'status' to 'A' # Redirect to the changelist URL with the correct query string return redirect( f"{reverse('admin:Human_Resource_Management_person_changelist')}?{query.urlencode()}" ) # Call the original changelist_view method return super().changelist_view(request, extra_context=extra_context) # Register your admin class with the associated model admin.site.register(Person, PersonAdmin) </code>
from django.contrib import admin
from django.shortcuts import redirect
from django.urls import reverse
from .models import Person  # Replace with your actual model

class PersonAdmin(admin.ModelAdmin):
    list_display = ('name', 'status')  # Adjust fields to match your model
    list_filter = ('status',)  # Adjust filters to match your model

    def get_queryset(self, request):
        # Get the original queryset without any default filtering
        qs = super().get_queryset(request)
        return qs

    def changelist_view(self, request, extra_context=None):
        # Check if any filters related to 'status' are already applied by looking at request.GET
        if 'status' not in request.GET and 'status__exact' not in request.GET:
            # Redirect to the same changelist URL with the default filter applied
            query = request.GET.copy()  # Make a mutable copy of GET parameters
            query['status'] = 'A'  # Set the default filter value for 'status' to 'A'
            
            # Redirect to the changelist URL with the correct query string
            return redirect(
                f"{reverse('admin:Human_Resource_Management_person_changelist')}?{query.urlencode()}"
            )

        # Call the original changelist_view method
        return super().changelist_view(request, extra_context=extra_context)

# Register your admin class with the associated model
admin.site.register(Person, PersonAdmin)

The code works initially: when the changelist view loads without any filters, it defaults to status=A. However, if a user selects another filter, such as status=B, the URL ends up looking like this:
?status=A&status__exact=B
This results in both filters being applied simultaneously, which is incorrect. I want to only have the user-selected filter (e.g., status=B) applied and not keep the default filter (status=A) in the URL.

What I’ve Tried:
I tried modifying request.GET directly, but that caused an AttributeError since request.GET is immutable.
I also tried more complex condition checks, but I can’t seem to prevent the status=A filter from staying in the URL when another filter is selected.
What I’m Looking For:
I’m looking for a solution that:

Applies the default filter (status=A) only when no other status filter is present.
Removes the default filter when the user selects a different filter (status=B) to avoid conflicting filters in the URL.
Any insights or suggestions on how to achieve this would be greatly appreciated!

Thank you!

1

The filter throws out the previous one with the same name. But you use status, not status__exact, hence the problem. Use status__exact instead:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class PersonAdmin(admin.ModelAdmin):
# …
def changelist_view(self, request, extra_context=None):
if 'status' not in request.GET and 'status__exact' not in request.GET:
# Redirect to the same changelist URL with the default filter applied
query = request.GET.copy() # Make a mutable copy of GET parameters
query['<strong>status__exact</strong>'] = 'A' # Set the default filter value for 'status' to 'A'
# Redirect to the changelist URL with the correct query string
return redirect(
f"{reverse('admin:Human_Resource_Management_person_changelist')}?{query.urlencode()}"
)
# Call the original changelist_view method
return super().changelist_view(request, extra_context=extra_context)</code>
<code>class PersonAdmin(admin.ModelAdmin): # … def changelist_view(self, request, extra_context=None): if 'status' not in request.GET and 'status__exact' not in request.GET: # Redirect to the same changelist URL with the default filter applied query = request.GET.copy() # Make a mutable copy of GET parameters query['<strong>status__exact</strong>'] = 'A' # Set the default filter value for 'status' to 'A' # Redirect to the changelist URL with the correct query string return redirect( f"{reverse('admin:Human_Resource_Management_person_changelist')}?{query.urlencode()}" ) # Call the original changelist_view method return super().changelist_view(request, extra_context=extra_context)</code>
class PersonAdmin(admin.ModelAdmin):
    # …

    def changelist_view(self, request, extra_context=None):
        if 'status' not in request.GET and 'status__exact' not in request.GET:
            # Redirect to the same changelist URL with the default filter applied
            query = request.GET.copy()  # Make a mutable copy of GET parameters
            query['status__exact'] = 'A'  # Set the default filter value for 'status' to 'A'

            # Redirect to the changelist URL with the correct query string
            return redirect(
                f"{reverse('admin:Human_Resource_Management_person_changelist')}?{query.urlencode()}"
            )

        # Call the original changelist_view method
        return super().changelist_view(request, extra_context=extra_context)

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật