Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
wants to merge 2 commits into from

3 participants

@selwin

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):
self.assertEqual(m.file.name, 'tests/\u6211\u96bb\u6c23\u588a\u8239\u88dd\u6eff\u6652\u9c54.txt')
m.delete()
+ def test_incorrect_instance_raises_exception(self):
+ # ModelForm with incorrect instance raises ValueError on init ##########
@apollo13 Owner

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

@selwin
selwin added a note

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?

@apollo13 Owner

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
selwin added a note

@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
@timgraham
Owner

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

@timgraham timgraham closed this
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.
View
3  django/forms/models.py
@@ -236,6 +236,9 @@ def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
self.instance = opts.model()
object_data = {}
else:
+ if not isinstance(instance, opts.model):
+ raise ValueError('"instance" must be an instance of {0}'.format(
+ opts.model._meta.object_name))
self.instance = instance
object_data = model_to_dict(instance, opts.fields, opts.exclude)
# if initial was provided, it should override the values from instance
View
10 tests/regressiontests/forms/tests/models.py
@@ -106,7 +106,7 @@ def test_initial_instance_value(self):
class FormsModelTestCase(TestCase):
def test_unicode_filename(self):
- # FileModel with unicode filename and data #########################
+ # FileModel with unicode filename and data
f = FileForm(data={}, files={'file1': SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह'.encode('utf-8'))}, auto_id=False)
self.assertTrue(f.is_valid())
self.assertTrue('file1' in f.cleaned_data)
@@ -114,8 +114,12 @@ def test_unicode_filename(self):
self.assertEqual(m.file.name, 'tests/\u6211\u96bb\u6c23\u588a\u8239\u88dd\u6eff\u6652\u9c54.txt')
m.delete()
+ def test_incorrect_instance_raises_exception(self):
+ # ModelForm with incorrect instance raises ValueError on init
+ self.assertRaises(ValueError, ChoiceFieldForm, instance=Group)
+
def test_boundary_conditions(self):
- # Boundary conditions on a PostitiveIntegerField #########################
+ # Boundary conditions on a PostitiveIntegerField
class BoundaryForm(ModelForm):
class Meta:
model = BoundaryModel
@@ -128,7 +132,7 @@ class Meta:
self.assertFalse(f.is_valid())
def test_formfield_initial(self):
- # Formfield initial values ########
+ # Formfield initial values
# If the model has default values for some fields, they are used as the formfield
# initial values.
class DefaultsForm(ModelForm):
Something went wrong with that request. Please try again.