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
This commit is contained in:
Joseph Kocherhans 2010-02-23 14:59:30 +00:00
parent 7aeb37d610
commit 8da76ffeaa
2 changed files with 8 additions and 1 deletions

View File

@ -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 = {}

View File

@ -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)