From 8da76ffeaa47547edb24f99a55f8c9e1a8986b90 Mon Sep 17 00:00:00 2001 From: Joseph Kocherhans Date: Tue, 23 Feb 2010 14:59:30 +0000 Subject: [PATCH] Fixed #12285. ModelForm raises a more informative error if it doesn't have a model class defined. Thanks, tobias. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12526 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/forms/models.py | 2 ++ tests/regressiontests/model_forms_regress/tests.py | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/django/forms/models.py b/django/forms/models.py index e292c1b9a7..430dd4d7cc 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -236,6 +236,8 @@ class BaseModelForm(BaseForm): empty_permitted=False, instance=None): opts = self._meta if instance is None: + if opts.model is None: + raise ValueError('ModelForm has no model class specified.') # if we didn't get an instance, instantiate a new one self.instance = opts.model() object_data = {} diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py index 3eeecb28e4..c923a12e15 100644 --- a/tests/regressiontests/model_forms_regress/tests.py +++ b/tests/regressiontests/model_forms_regress/tests.py @@ -145,5 +145,10 @@ class CustomModelFormSaveMethod(TestCase): data = {'name': 'anonymous'} form = RealPersonForm(data) self.assertEqual(form.is_valid(), False) - self.assertEqual(form.errors['__all__'], ['Please specify a real name.']) + self.assertEqual(form.errors['__all__'], ['Please specify a real name.']) +class ModelClassTests(TestCase): + def test_no_model_class(self): + class NoModelModelForm(forms.ModelForm): + pass + self.assertRaises(ValueError, NoModelModelForm)