Opened 10 years ago

Last modified 10 years ago

#23055 closed Bug

Filters don't use ModelAdmin get_queryset() — at Version 1

Reported by: Ramiro Morales Owned by: nobody
Component: contrib.admin Version: dev
Severity: Normal Keywords: admin filters list_filter multi-db get_queryset modeladmin
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Ramiro Morales)

If one is using a ModelAdmin with a custom get_queryset() method (e.g. as described in https://docs.djangoproject.com/en/1.7/topics/db/multi-db/#exposing-multiple-databases-in-django-s-admin-interface to handle routing of models hosted in a multi-DB setup), displaying the change list works as expected.

But when usage of the list_filter feature is added, a DB error is generated reporting that no table object for the model at hand is found in the default Django DB.

This is because the filter machinery doesn't use the custom ModelAdmin-dictated QuerySet but the model's default manager all() method:

(https://github.com/django/django/blob/011abb7d96c75f6154c15a8a0f997d8c27698679/django/contrib/admin/filters.py#L364)

    queryset = parent_model._default_manager.all()

Replacing it with:

diff --git a/django/contrib/admin/filters.py b/django/contrib/admin/filters.py
index d5f31ab..d04d5cc 100644
--- a/django/contrib/admin/filters.py
+++ b/django/contrib/admin/filters.py
@@ -361,7 +361,7 @@ class AllValuesFieldListFilter(FieldListFilter):
         self.lookup_val_isnull = request.GET.get(self.lookup_kwarg_isnull,
                                                  None)
         parent_model, reverse_path = reverse_field_path(model, field_path)
-        queryset = parent_model._default_manager.all()
+        queryset = model_admin.get_queryset(request)
         # optional feature: limit choices base on existing relationships
         # queryset = queryset.complex_filter(
         #    {'%s__isnull' % reverse_path: False})

solves the problem.

Change History (1)

comment:1 by Ramiro Morales, 10 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top