From 20ddc3b81d849be8fc7961081ccb7a6d7e454a89 Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Wed, 3 Mar 2021 15:06:09 +0100 Subject: [PATCH] Fixed #32494 -- Adjusted system check for raw_id_fields to warn about Field.attname. --- django/contrib/admin/checks.py | 8 ++++++++ tests/modeladmin/test_checks.py | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/django/contrib/admin/checks.py b/django/contrib/admin/checks.py index 35760a1177..045aaca346 100644 --- a/django/contrib/admin/checks.py +++ b/django/contrib/admin/checks.py @@ -239,6 +239,14 @@ class BaseModelAdminChecks: except FieldDoesNotExist: return refer_to_missing_field(field=field_name, option=label, obj=obj, id='admin.E002') else: + # Using attname is not supported. + if field.name != field_name: + return refer_to_missing_field( + field=field_name, + option=label, + obj=obj, + id='admin.E002', + ) if not field.many_to_many and not isinstance(field, models.ForeignKey): return must_be('a foreign key or a many-to-many field', option=label, obj=obj, id='admin.E003') else: diff --git a/tests/modeladmin/test_checks.py b/tests/modeladmin/test_checks.py index 46d18b32ca..f224770948 100644 --- a/tests/modeladmin/test_checks.py +++ b/tests/modeladmin/test_checks.py @@ -84,6 +84,18 @@ class RawIdCheckTests(CheckTestCase): self.assertIsValid(TestModelAdmin, ValidationTestModel) + def test_field_attname(self): + class TestModelAdmin(ModelAdmin): + raw_id_fields = ['band_id'] + + self.assertIsInvalid( + TestModelAdmin, + ValidationTestModel, + "The value of 'raw_id_fields[0]' refers to 'band_id', which is " + "not a field of 'modeladmin.ValidationTestModel'.", + 'admin.E002', + ) + class FieldsetsCheckTests(CheckTestCase):