Skip to content

Commit

Permalink
Fixed #12285. ModelForm raises a more informative error if it doesn't…
Browse files Browse the repository at this point in the history
… have a model class defined. Thanks, tobias.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12526 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jkocherhans committed Feb 23, 2010
1 parent 7aeb37d commit 8da76ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 2 additions & 0 deletions django/forms/models.py
Expand Up @@ -236,6 +236,8 @@ def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
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 = {}
Expand Down
7 changes: 6 additions & 1 deletion tests/regressiontests/model_forms_regress/tests.py
Expand Up @@ -145,5 +145,10 @@ def test_string_message(self):
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)

0 comments on commit 8da76ff

Please sign in to comment.