Django archive user if clicked for detail page view an error appears

so i have this user model and in admin.py which ofcourse is used by my client as admin page view and recently my client requested that the user/s who has been archived should not show in the active list page of in the django admin ,but can be viewed if filtered so i put the functionality in my base "

get_queryset

"

which is as follow

def get_queryset(self, request):
    qs = super().get_queryset(request)

    # detecting if archive date filter is applied
    archive_filter_applied = any(
        key.startswith("archived_at__")
        for key in request.GET
    )

    # default behaviour which excludes archived users
    if not archive_filter_applied:
            qs = qs.filter(archived_at__isnull=True)

    if request.user.is_superuser:
        return qs

    # permission gate
    if not request.user.has_perm("api.view_user"):
        return qs.none()

    therapist_qs = qs.filter(
        Q(treatment_therapist=request.user) |
        Q(assessment_therapist=request.user)
    )

    # data-driven therapist rule
    if therapist_qs.exists():
        return therapist_qs

    return qs

so what happens is as normal the users are shown who are active and not archived but if filter through a filter only those are shown which are archived so till here all is fine but when the archived user is clicked in the list page to see his details page an error appears like this
" Client with ID “375” doesn’t exist. Perhaps it was deleted?"
i have this feeling that i have a bug in this part of code but i cant figure it out

p.s i know the question is long but this the most precise way i could ask

I think the issue is that once going to detail view, there is no more `archived_at__` in GET. Although accessing directly to the GET param without the cleanup of django is unsafe IMHO.

I would have gone for a custom SimpleListFilter :

from django.contrib import admin
from django.contrib.auth import get_user_model


class ArchivedListFilter(admin.SimpleListFilter):
    title = "archived status"
    parameter_name = "active_only"

    def lookups(self, request, model_admin):
        return [
            (True, "All (archived included)"),
        ]

    def queryset(self, request, queryset):
        # When using it, we have all users
        if self.value():
            return queryset
        # When not using it, filter the users
        # return queryset.filter(archived_at__isnull=True) # uncomment me !
        return queryset.filter(username__icontains="b")

    def choices(self, changelist):
        first = True
        for c in super().choices(changelist):
            if first:
                # The first one is the _('All') choice, we change its display
                c["display"] = "Active Only"
                first = False
            yield c


admin.site.unregister(get_user_model())

@admin.register(get_user_model())
class UserAdmin(admin.ModelAdmin):
    list_filter = (ArchivedListFilter,)


    def get_queryset(self, request):
        qs = super().get_queryset(request)

        # detecting if archive date filter is applied
        archive_filter_applied = (any(
            key.startswith("archived_at__")
            for key in request.GET) or "archived_at__" in request.GET.get("_changelist_filters", "")
        )

        # default behaviour which excludes archived users
        if not archive_filter_applied:
                qs = qs.filter(archived_at__isnull=True)

        if request.user.is_superuser:
            return qs

        # permission gate
        if not request.user.has_perm("api.view_user"):
            return qs.none()

        therapist_qs = qs.filter(
            Q(treatment_therapist=request.user) |
            Q(assessment_therapist=request.user)
        )

        # data-driven therapist rule
        if therapist_qs.exists():
            return therapist_qs

        return qs

so i came back to this or lets say this is the solution which now lets user see the detail page of the archived users also

do you think this is good ??

Вернуться на верх