#37187: Extraneous and confusing ModelAdmin select_related deprecation warnings
-------------------------------------+-------------------------------------
Reporter: Mike | Owner: Mike Edmunds
Edmunds |
Type: Bug | Status: assigned
Component: | Version: 6.1
contrib.admin | Keywords: ModelAdmin,
Severity: Normal | deprecation
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Some of the deprecation warnings introduced in
122f0b62a1effa558aad67a62e5b0d84a49cdc23 (as a follow-up to #36593):
* Are issued against Django internals or wsgiref/handlers.py (or whatever
code is serving the admin site)—''not'' against the code that is
responsible for the deprecated behavior.
* Sometimes include an extra warning that "Calling select_related() with
no arguments is deprecated," even though select_related() is not called
anywhere in the user's code.
To reproduce (Django 6.1b1, run with `PYTHONWARNINGS=always` to see all
deprecations):
1. Have a `models.Author` and `models.Book` with a foreign key to author.
2. Define `admin.BookAdmin` with a `get_select_related()` method that
returns the deprecated value `True`:
{{{#!python
from django.contrib import admin
from .models import Author, Book
class BookAdmin(admin.ModelAdmin):
list_display = ['title', 'author__name']
def get_list_select_related(self, request):
return True # deprecated return value
admin.site.register(Author)
admin.site.register(Book, BookAdmin)
}}}
3. Run the devserver and visit `/admin/yourapp/books/`
Results: the warning about returning True from get_list_select_related()
points to Django's internals, and there's an extra warning about
wsgiref/handlers.py calling select_related() with no arguments:
{{{#!text
.venv/lib/python3.14/site-packages/django/contrib/admin/options.py:930:
RemovedInDjango70Warning: Returning True from
ModelAdmin.get_list_select_related() is deprecated. Return False or a list
or tuple of fields to fetch instead.
warnings.warn(
.pyenv/versions/3.14.3/lib/python3.14/wsgiref/handlers.py:137:
RemovedInDjango70Warning: Calling select_related() with no arguments is
deprecated. Specify the fields to fetch instead.
self.result = application(self.environ, self.start_response)
}}}
Expected: ideally, something that points to the BookAdmin implementation
(and nothing about calling select_related()):
{{{#!text
.../example-project/yourapp/admin.py:7: RemovedInDjango70Warning:
Returning True from ModelAdmin.get_list_select_related() is deprecated.
Return False or a list or tuple of fields to fetch instead.
def get_list_select_related(self, request):
}}}
There are two issues here:
A. ModelAdmin.get_changelist_instance() is trying to warn about an
overridden subclass method that has already been called so is no longer on
the stack. (It's also missing `skip_file_prefixes`, which is why it's
blaming Django internals. But adding that would just move the blame to
wsgiref—still not helpful.)
B. QuerySet.select_related() is unconditionally issuing a deprecation
warning, even when it's being used internally to implement a deprecated
feature elsewhere in Django (like in ModelAdmin).
The second problem is easily solved: we implemented
`warn_about_external_use()` specifically for this use case.
The first problem is probably solvable by using `warnings.warn_explicit()`
to point to the subclass method that is actually at fault. (I'm looking
into that now.) If that doesn't work, we could change the warning message
to include the actual subclass name: `Returning True from
yourapp.admin.BookAdmin.get_list_select_related() is deprecated.`
Also:
* If you replace `BookAdmin.get_list_select_related()` with
`list_select_related = True`, you get a nice startup-time warning that
points at the `class BookAdmin` definition in your own admin.py. But you
still get the extraneous `select_related()` warning in wsgiref when
visiting the view.
* If you load the admin view from a test, the warnings will point to the
`self.client.get("/admin/core/books/")` line instead of wsgiref. (That at
least gives you some idea which code might be the problem.)
* I suspect there's a similar issue with the warning for overriding
get_actions()/get_action_choices() without the 'action_location' parameter
(from #12090 - f30acb184f75fd9260cfd6ddc48a3bbbd49f9c1d).
* The extraneous select_related() warning occurs in Django's tests, but is
being suppressed by #37072. (The PR for that issue made it visible, and
pulling that thread lead to this.)
--
Ticket URL: <
https://code.djangoproject.com/ticket/37187>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.