diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py index 05e5c6d300..d1f8de24e6 100644 --- a/django/contrib/admin/validation.py +++ b/django/contrib/admin/validation.py @@ -149,7 +149,7 @@ def validate(cls, model): validate_inline(inline, cls, model) def validate_inline(cls, parent, parent_model): - + # model is already verified to exist and be a Model if cls.fk_name: # default value is None f = get_field(cls, cls.model, cls.model._meta, 'fk_name', cls.fk_name) @@ -196,6 +196,9 @@ def validate_base(cls, model): check_isseq(cls, 'fields', cls.fields) for field in cls.fields: check_formfield(cls, model, opts, 'fields', field) + f = get_field(cls, model, opts, 'fields', field) + if isinstance(f, models.ManyToManyField) and not f.rel.through._meta.auto_created: + raise ImproperlyConfigured("'%s.fields' can't include the ManyToManyField field '%s' because '%s' manually specifies a 'through' model." % (cls.__name__, field, field)) if cls.fieldsets: raise ImproperlyConfigured('Both fieldsets and fields are specified in %s.' % cls.__name__) if len(cls.fields) > len(set(cls.fields)): diff --git a/tests/regressiontests/admin_validation/models.py b/tests/regressiontests/admin_validation/models.py index 93e3e065e6..924cf69d4d 100644 --- a/tests/regressiontests/admin_validation/models.py +++ b/tests/regressiontests/admin_validation/models.py @@ -108,7 +108,18 @@ Exception: ha >>> validate_inline(TwoAlbumFKAndAnEInline, None, Album) -# Regression test for #12203 -- If the explicitly provided through model +# Regression test for #12203 - Fail more gracefully when a M2M field that +# specifies the 'through' option is included in the 'fields' ModelAdmin option. + +>>> class BookAdmin(admin.ModelAdmin): +... fields = ['authors'] + +>>> validate(BookAdmin, Book) +Traceback (most recent call last): + ... +ImproperlyConfigured: 'BookAdmin.fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model. + +# Regression test for #12209 -- If the explicitly provided through model # is specified as a string, the admin should still be able use # Model.m2m_field.through