diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2009-03-31 21:42:47 +0000 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2009-03-31 21:42:47 +0000 |
| commit | 67a58801623cccd55ecdd7d21f7b2dc487175c03 (patch) | |
| tree | e5cdf263fe68bfa23d0fc2fefbb32c36f89ff5d3 /django | |
| parent | 09953968172935ac5665cf75f0de87961d1741fe (diff) | |
[1.0.X] Fixed #10413: RelatedManager.add no longer fails silenty when trying to add an object of the wrong type. Thanks, dgouldin.
Backport of r10226 from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10293 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/contenttypes/generic.py | 2 | ||||
| -rw-r--r-- | django/db/models/fields/related.py | 5 |
2 files changed, 7 insertions, 0 deletions
diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py index 8dd735d15e..be67557805 100644 --- a/django/contrib/contenttypes/generic.py +++ b/django/contrib/contenttypes/generic.py @@ -253,6 +253,8 @@ def create_generic_related_manager(superclass): def add(self, *objs): for obj in objs: + if not isinstance(obj, self.model): + raise TypeError, "'%s' instance expected" % self.model._meta.object_name setattr(obj, self.content_type_field_name, self.content_type) setattr(obj, self.object_id_field_name, self.pk_val) obj.save() diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index c91bd22654..4a9ed32470 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -325,6 +325,8 @@ class ForeignRelatedObjectsDescriptor(object): def add(self, *objs): for obj in objs: + if not isinstance(obj, self.model): + raise TypeError, "'%s' instance expected" % self.model._meta.object_name setattr(obj, rel_field.name, instance) obj.save() add.alters_data = True @@ -445,11 +447,14 @@ def create_many_related_manager(superclass, through=False): # If there aren't any objects, there is nothing to do. if objs: + from django.db.models.base import Model # Check that all the objects are of the right type new_ids = set() for obj in objs: if isinstance(obj, self.model): new_ids.add(obj._get_pk_val()) + elif isinstance(obj, Model): + raise TypeError, "'%s' instance expected" % self.model._meta.object_name else: new_ids.add(obj) # Add the newly created or already existing objects to the join table. |
