From 909e002808135ee943643e6b1866e9aa5cb0b42d Mon Sep 17 00:00:00 2001 From: Karen Tracey Date: Sun, 22 May 2011 15:51:22 +0000 Subject: [PATCH] Fix UnboundLocalError than could occur during ModelAdmin validation. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16262 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/validation.py | 6 +++--- tests/regressiontests/admin_validation/tests.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py index b4f2b9d0dc..33acebf0b6 100644 --- a/django/contrib/admin/validation.py +++ b/django/contrib/admin/validation.py @@ -247,9 +247,9 @@ def validate_fields_spec(cls, model, opts, flds, label): try: f = opts.get_field(field) except models.FieldDoesNotExist: - # If we can't find a field on the model that matches, - # it could be an extra field on the form. - pass + # If we can't find a field on the model that matches, it could be an + # extra field on the form; nothing to check so move on to the next field. + continue if isinstance(f, models.ManyToManyField) and not f.rel.through._meta.auto_created: raise ImproperlyConfigured("'%s.%s' " "can't include the ManyToManyField field '%s' because " diff --git a/tests/regressiontests/admin_validation/tests.py b/tests/regressiontests/admin_validation/tests.py index a80820cb67..2cf3c60062 100644 --- a/tests/regressiontests/admin_validation/tests.py +++ b/tests/regressiontests/admin_validation/tests.py @@ -256,3 +256,19 @@ class ValidationTestCase(TestCase): fields = ['title', 'extra_data'] validate(FieldsOnFormOnlyAdmin, Song) + + def test_non_model_first_field(self): + """ + Regression for ensuring ModelAdmin.field can handle first elem being a + non-model field (test fix for UnboundLocalError introduced with r16225). + """ + class SongForm(forms.ModelForm): + extra_data = forms.CharField() + class Meta: + model = Song + + class FieldsOnFormOnlyAdmin(admin.ModelAdmin): + form = SongForm + fields = ['extra_data', 'title'] + + validate(FieldsOnFormOnlyAdmin, Song)