From 210930858d1e4a16733b6f443af5dcb1cf3d1602 Mon Sep 17 00:00:00 2001
From: Daniel Barreto <daniel@gia.usb.ve>
Date: Sat, 12 Nov 2011 20:33:43 -0430
Subject: [PATCH] Fixes #15877 -- ModelForm will raise ValueError if not Meta model specified.
---
django/forms/models.py | 4 ++--
tests/modeltests/model_forms/tests.py | 14 ++++++++++++++
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/django/forms/models.py b/django/forms/models.py
index b65f067..a9d4129 100644
a
|
b
|
class BaseModelForm(BaseForm):
|
227 | 227 | initial=None, error_class=ErrorList, label_suffix=':', |
228 | 228 | empty_permitted=False, instance=None): |
229 | 229 | opts = self._meta |
| 230 | if opts.model is None: |
| 231 | raise ValueError('ModelForm has no model class specified.') |
230 | 232 | if instance is None: |
231 | | if opts.model is None: |
232 | | raise ValueError('ModelForm has no model class specified.') |
233 | 233 | # if we didn't get an instance, instantiate a new one |
234 | 234 | self.instance = opts.model() |
235 | 235 | object_data = {} |
diff --git a/tests/modeltests/model_forms/tests.py b/tests/modeltests/model_forms/tests.py
index c4abdbb..a837931 100644
a
|
b
|
class ShortCategory(forms.ModelForm):
|
129 | 129 | slug = forms.CharField(max_length=5) |
130 | 130 | url = forms.CharField(max_length=3) |
131 | 131 | |
| 132 | class Meta: |
| 133 | model = Category |
| 134 | |
132 | 135 | class ImprovedArticleForm(forms.ModelForm): |
133 | 136 | class Meta: |
134 | 137 | model = ImprovedArticle |
… |
… |
class ModelFormBaseTest(TestCase):
|
181 | 184 | self.assertEqual(BaseCategoryForm.base_fields.keys(), |
182 | 185 | ['name', 'slug', 'url']) |
183 | 186 | |
| 187 | def test_invalid_meta_model(self): |
| 188 | class InvalidModelForm(forms.ModelForm): |
| 189 | class Meta: |
| 190 | pass # no model. |
| 191 | # can't create new form |
| 192 | with self.assertRaises(ValueError): |
| 193 | f = InvalidModelForm() |
| 194 | # even if you provide a model instance |
| 195 | with self.assertRaises(ValueError): |
| 196 | f = InvalidModelForm(instance=Category) |
| 197 | |
184 | 198 | def test_extra_fields(self): |
185 | 199 | class ExtraFields(BaseCategoryForm): |
186 | 200 | some_extra_field = forms.BooleanField() |