Fixed #32494 -- Adjusted system check for raw_id_fields to warn about Field.attname.

This commit is contained in:
Hasan Ramezani 2021-03-03 15:06:09 +01:00 committed by Mariusz Felisiak
parent 1da54bfe7d
commit 20ddc3b81d
2 changed files with 20 additions and 0 deletions

View File

@ -239,6 +239,14 @@ class BaseModelAdminChecks:
except FieldDoesNotExist: except FieldDoesNotExist:
return refer_to_missing_field(field=field_name, option=label, obj=obj, id='admin.E002') return refer_to_missing_field(field=field_name, option=label, obj=obj, id='admin.E002')
else: 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): 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') return must_be('a foreign key or a many-to-many field', option=label, obj=obj, id='admin.E003')
else: else:

View File

@ -84,6 +84,18 @@ class RawIdCheckTests(CheckTestCase):
self.assertIsValid(TestModelAdmin, ValidationTestModel) 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): class FieldsetsCheckTests(CheckTestCase):