Ticket #12237: 12237-m2m-through-modeladmin-fieldsets.diff

File 12237-m2m-through-modeladmin-fieldsets.diff, 2.4 KB (added by Ramiro Morales, 10 years ago)

Patch that implements the validation also for the fielsets ModelAdmin option, includes tests.

  • django/contrib/admin/validation.py

    diff -r cd02ae3d0202 django/contrib/admin/validation.py
    a b  
    222222            raise ImproperlyConfigured('There are duplicate field(s) in %s.fieldsets' % cls.__name__)
    223223        for field in flattened_fieldsets:
    224224            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))
    225231
    226232    # form
    227233    if hasattr(cls, 'form') and not issubclass(cls.form, BaseModelForm):
  • tests/regressiontests/admin_validation/models.py

    diff -r cd02ae3d0202 tests/regressiontests/admin_validation/models.py
    a b  
    108108
    109109>>> validate_inline(TwoAlbumFKAndAnEInline, None, Album)
    110110
    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.
    113114
    114115>>> class BookAdmin(admin.ModelAdmin):
    115116...     fields = ['authors']
     
    118119Traceback (most recent call last):
    119120    ...
    120121ImproperlyConfigured: '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)
     130Traceback (most recent call last):
     131   ...
     132ImproperlyConfigured: 'FieldsetBookAdmin.fieldsets' can't include the ManyToManyField field 'authors' because 'authors' manually specifies a 'through' model.
    121133
    122134# Regression test for #12209 -- If the explicitly provided through model
    123135# is specified as a string, the admin should still be able use
Back to Top