Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fix for #6702: ModelForm now checks instance on __init__ #296

Closed
wants to merge 2 commits into from

3 participants

Selwin Ong Tim Graham Florian Apolloner
Selwin Ong

Hi there,

Attached is a patch and testcase that fixes https://code.djangoproject.com/ticket/6702 . Let me know if there's anything else that needs changing.

Best,
Selwin

tests/regressiontests/forms/tests/models.py
@@ -114,6 +114,10 @@ def test_unicode_filename(self):
114 114
         self.assertEqual(m.file.name, 'tests/\u6211\u96bb\u6c23\u588a\u8239\u88dd\u6eff\u6652\u9c54.txt')
115 115
         m.delete()
116 116
 
  117
+    def test_incorrect_instance_raises_exception(self):
  118
+        # ModelForm with incorrect instance raises ValueError on init ##########
4
Florian Apolloner Owner
apollo13 added a note August 31, 2012

What's with the hashes (#) at the end?

Selwin Ong
selwin added a note August 31, 2012

The tests surrounding it have comments ending with multiple hashes so I was just following those (Lines 109 and 122). Want me to remove them?

Florian Apolloner Owner
apollo13 added a note August 31, 2012

Yes that would be great (also the other occurences in that file). Btw Malcom said the assert was okay: https://code.djangoproject.com/ticket/6702#comment:3 -- Why do you use an insinstance check in your patch?

Selwin Ong
selwin added a note August 31, 2012

@apollo13 I just updated my branch to clean the extra hashes. I raised ValueError because it's more consistent with the rest of ModelForm (raises ValueError when no model is specified and when save is called when the form doesn't validate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Tim Graham
Owner

Ticket has been marked as "won't fix."

Tim Graham timgraham closed this May 31, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
3  django/forms/models.py
@@ -236,6 +236,9 @@ def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
236 236
             self.instance = opts.model()
237 237
             object_data = {}
238 238
         else:
  239
+            if not isinstance(instance, opts.model):
  240
+                raise ValueError('"instance" must be an instance of {0}'.format(
  241
+                                    opts.model._meta.object_name))
239 242
             self.instance = instance
240 243
             object_data = model_to_dict(instance, opts.fields, opts.exclude)
241 244
         # if initial was provided, it should override the values from instance
10  tests/regressiontests/forms/tests/models.py
@@ -106,7 +106,7 @@ def test_initial_instance_value(self):
106 106
 
107 107
 class FormsModelTestCase(TestCase):
108 108
     def test_unicode_filename(self):
109  
-        # FileModel with unicode filename and data #########################
  109
+        # FileModel with unicode filename and data
110 110
         f = FileForm(data={}, files={'file1': SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह'.encode('utf-8'))}, auto_id=False)
111 111
         self.assertTrue(f.is_valid())
112 112
         self.assertTrue('file1' in f.cleaned_data)
@@ -114,8 +114,12 @@ def test_unicode_filename(self):
114 114
         self.assertEqual(m.file.name, 'tests/\u6211\u96bb\u6c23\u588a\u8239\u88dd\u6eff\u6652\u9c54.txt')
115 115
         m.delete()
116 116
 
  117
+    def test_incorrect_instance_raises_exception(self):
  118
+        # ModelForm with incorrect instance raises ValueError on init
  119
+        self.assertRaises(ValueError, ChoiceFieldForm, instance=Group)
  120
+
117 121
     def test_boundary_conditions(self):
118  
-        # Boundary conditions on a PostitiveIntegerField #########################
  122
+        # Boundary conditions on a PostitiveIntegerField
119 123
         class BoundaryForm(ModelForm):
120 124
             class Meta:
121 125
                 model = BoundaryModel
@@ -128,7 +132,7 @@ class Meta:
128 132
         self.assertFalse(f.is_valid())
129 133
 
130 134
     def test_formfield_initial(self):
131  
-        # Formfield initial values ########
  135
+        # Formfield initial values
132 136
         # If the model has default values for some fields, they are used as the formfield
133 137
         # initial values.
134 138
         class DefaultsForm(ModelForm):
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.