Opened 18 minutes ago
Last modified 16 minutes ago
#37536 assigned Bug
Admin changelist raises FieldError when only() defers a foreign key in list_display
| Reported by: | Cleisson | Owned by: | Cleisson |
|---|---|---|---|
| Component: | contrib.admin | Version: | 6.1 |
| Severity: | Release blocker | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
After upgrading from Django 6.0.10 to 6.1, an existing admin changelist can fail to render when ModelAdmin.get_queryset() uses only() and list_display includes a deferred foreign key. The changelist should display the row and load the deferred relation on access, as it does on 6.0.10.
Save the script below as repro.py and run python repro.py in an environment with Django installed. It uses in-memory SQLite and needs no project or database setup. The Control disables the implicit join with list_select_related = ().
from types import SimpleNamespace import django from django.conf import settings settings.configure( SECRET_KEY="demo", ROOT_URLCONF=__name__, DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}}, INSTALLED_APPS=["django.contrib.contenttypes", "django.contrib.auth", "django.contrib.admin"], ) urlpatterns = [] django.setup() from django.contrib import admin from django.db import connection, models from django.test import RequestFactory class Author(models.Model): name = models.CharField(max_length=20) class Meta: app_label = "demo" def __str__(self): return self.name class Book(models.Model): title = models.CharField(max_length=40) author = models.ForeignKey(Author, null=True, on_delete=models.CASCADE) class Meta: app_label = "demo" class BookAdmin(admin.ModelAdmin): list_display = ("title", "author") def get_queryset(self, request): return super().get_queryset(request).only("title") with connection.schema_editor() as schema: schema.create_model(Author) schema.create_model(Book) Book.objects.create(title="Novel", author=Author.objects.create(name="Writer")) request = RequestFactory().get("/") request.user = SimpleNamespace(is_active=True, is_staff=True, has_perm=lambda *args: True) for form in [type("Control",(BookAdmin,),{"list_select_related": ()}), BookAdmin]: changelist = form(Book, admin.AdminSite()).get_changelist_instance(request) try: print(django.get_version(), form.__name__, [(obj.title,str(obj.author)) for obj in changelist.result_list], flush=True) except Exception as exc: print(django.get_version(), form.__name__, type(exc).__name__, str(exc), flush=True)
On Django 6.0.10, both admin classes return [('Novel', 'Writer')]. On Django 6.1 and 6.1.3, the control still returns the row, but the default admin raises:
FieldError: Field Book.author cannot be both deferred and traversed using select_related at the same time.
The same result occurs on main at f14af5a88472ca10e2e94b37d6c17ee48cbdeb4d. The script passes on 4d455ae2d7689ce066dfffef9fc29a6f6d3ed33e and first fails at 0c634de4e6950ec6fb83ab420b4af9c645b28acd (#36593). That commit changed the default changelist to pass the displayed foreign key explicitly to select_related(). The ORM rejects the resulting query because only("title") defers that field.
A separate Django test exercising ModelAdmin.changelist_view() on main also fails with the same FieldError before the response can render (one test, SQLite).
This is a release-blocker candidate because a Django upgrade can leave the affected model's entire changelist unavailable until the application changes its admin configuration. Setting list_select_related = () restores access in the example.
OpenAI Codex with GPT-6.2 Astra and GPT-6.2 Sol assisted with investigating the regression, writing and running the reproduction.