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)