Ticket #15877: 0001-Fixes-15877-ModelForm-will-raise-ValueError-if-not-M.2.patch

File 0001-Fixes-15877-ModelForm-will-raise-ValueError-if-not-M.2.patch, 2.3 KB (added by Daniel Barreto, 13 years ago)
  • django/forms/models.py

    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):  
    227227                 initial=None, error_class=ErrorList, label_suffix=':',
    228228                 empty_permitted=False, instance=None):
    229229        opts = self._meta
     230        if opts.model is None:
     231            raise ValueError('ModelForm has no model class specified.')
    230232        if instance is None:
    231             if opts.model is None:
    232                 raise ValueError('ModelForm has no model class specified.')
    233233            # if we didn't get an instance, instantiate a new one
    234234            self.instance = opts.model()
    235235            object_data = {}
  • tests/modeltests/model_forms/tests.py

    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):  
    129129    slug = forms.CharField(max_length=5)
    130130    url = forms.CharField(max_length=3)
    131131
     132    class Meta:
     133        model = Category
     134
    132135class ImprovedArticleForm(forms.ModelForm):
    133136    class Meta:
    134137        model = ImprovedArticle
    class ModelFormBaseTest(TestCase):  
    181184        self.assertEqual(BaseCategoryForm.base_fields.keys(),
    182185                         ['name', 'slug', 'url'])
    183186
     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
    184198    def test_extra_fields(self):
    185199        class ExtraFields(BaseCategoryForm):
    186200            some_extra_field = forms.BooleanField()
Back to Top