Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ModelForm now checks whether instance is correct on __init__ (Fixes

  • Loading branch information...
commit 0208682cb6e9ae8211235785f02080ef6bc544b5 1 parent bfc380b
@selwin authored
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
4 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 ##########
+ self.assertRaises(ValueError, ChoiceFieldForm, instance=Group)
+
def test_boundary_conditions(self):
# Boundary conditions on a PostitiveIntegerField #########################
class BoundaryForm(ModelForm):
Please sign in to comment.
Something went wrong with that request. Please try again.