diff -r cd02ae3d0202 django/contrib/admin/validation.py
a
|
b
|
|
222 | 222 | raise ImproperlyConfigured('There are duplicate field(s) in %s.fieldsets' % cls.__name__) |
223 | 223 | for field in flattened_fieldsets: |
224 | 224 | check_formfield(cls, model, opts, "fieldsets[%d][1]['fields']" % idx, field) |
| 225 | try: |
| 226 | f = opts.get_field(field) |
| 227 | except models.FieldDoesNotExist: |
| 228 | continue |
| 229 | if isinstance(f, models.ManyToManyField) and not f.rel.through._meta.auto_created: |
| 230 | raise ImproperlyConfigured("'%s.fieldsets' can't include the ManyToManyField field '%s' because '%s' manually specifies a 'through' model." % (cls.__name__, field, field)) |
225 | 231 | |
226 | 232 | # form |
227 | 233 | if hasattr(cls, 'form') and not issubclass(cls.form, BaseModelForm): |
diff -r cd02ae3d0202 tests/regressiontests/admin_validation/models.py
a
|
b
|
|
108 | 108 | |
109 | 109 | >>> validate_inline(TwoAlbumFKAndAnEInline, None, Album) |
110 | 110 | |
111 | | # Regression test for #12203 - Fail more gracefully when a M2M field that |
112 | | # specifies the 'through' option is included in the 'fields' ModelAdmin option. |
| 111 | # Regression test for #12203/#12237 - Fail more gracefully when a M2M field that |
| 112 | # specifies the 'through' option is included in the 'fields' or the 'fieldsets' |
| 113 | # ModelAdmin options. |
113 | 114 | |
114 | 115 | >>> class BookAdmin(admin.ModelAdmin): |
115 | 116 | ... fields = ['authors'] |
… |
… |
|
118 | 119 | Traceback (most recent call last): |
119 | 120 | ... |
120 | 121 | ImproperlyConfigured: 'BookAdmin.fields' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model. |
| 122 | |
| 123 | >>> class FieldsetBookAdmin(admin.ModelAdmin): |
| 124 | ... fieldsets = ( |
| 125 | ... ('Header 1', {'fields': ('name',)}), |
| 126 | ... ('Header 2', {'fields': ('authors',)}), |
| 127 | ... ) |
| 128 | |
| 129 | >>> validate(FieldsetBookAdmin, Book) |
| 130 | Traceback (most recent call last): |
| 131 | ... |
| 132 | ImproperlyConfigured: 'FieldsetBookAdmin.fieldsets' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model. |
121 | 133 | |
122 | 134 | # Regression test for #12209 -- If the explicitly provided through model |
123 | 135 | # is specified as a string, the admin should still be able use |