Fixed #15877 -- Improved exception when ModelForm has no model class
Thanks theaspect at gmail.com for the report and volrath for the patch.
This commit is contained in:
parent
a05ab448f7
commit
cc53d9b30b
|
@ -233,9 +233,9 @@ class BaseModelForm(BaseForm):
|
||||||
initial=None, error_class=ErrorList, label_suffix=':',
|
initial=None, error_class=ErrorList, label_suffix=':',
|
||||||
empty_permitted=False, instance=None):
|
empty_permitted=False, instance=None):
|
||||||
opts = self._meta
|
opts = self._meta
|
||||||
if instance is None:
|
|
||||||
if opts.model is None:
|
if opts.model is None:
|
||||||
raise ValueError('ModelForm has no model class specified.')
|
raise ValueError('ModelForm has no model class specified.')
|
||||||
|
if instance is None:
|
||||||
# if we didn't get an instance, instantiate a new one
|
# if we didn't get an instance, instantiate a new one
|
||||||
self.instance = opts.model()
|
self.instance = opts.model()
|
||||||
object_data = {}
|
object_data = {}
|
||||||
|
|
|
@ -133,6 +133,9 @@ class ShortCategory(forms.ModelForm):
|
||||||
slug = forms.CharField(max_length=5)
|
slug = forms.CharField(max_length=5)
|
||||||
url = forms.CharField(max_length=3)
|
url = forms.CharField(max_length=3)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Category
|
||||||
|
|
||||||
class ImprovedArticleForm(forms.ModelForm):
|
class ImprovedArticleForm(forms.ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ImprovedArticle
|
model = ImprovedArticle
|
||||||
|
@ -277,6 +280,19 @@ class ModelFormBaseTest(TestCase):
|
||||||
['headline', 'slug', 'pub_date', 'writer', 'article', 'categories', 'status']
|
['headline', 'slug', 'pub_date', 'writer', 'article', 'categories', 'status']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_invalid_meta_model(self):
|
||||||
|
class InvalidModelForm(forms.ModelForm):
|
||||||
|
class Meta:
|
||||||
|
pass # no model
|
||||||
|
|
||||||
|
# Can't create new form
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
f = InvalidModelForm()
|
||||||
|
|
||||||
|
# Even if you provide a model instance
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
f = InvalidModelForm(instance=Category)
|
||||||
|
|
||||||
def test_subcategory_form(self):
|
def test_subcategory_form(self):
|
||||||
class SubCategoryForm(BaseCategoryForm):
|
class SubCategoryForm(BaseCategoryForm):
|
||||||
""" Subclassing without specifying a Meta on the class will use
|
""" Subclassing without specifying a Meta on the class will use
|
||||||
|
|
Loading…
Reference in New Issue