Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[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
  • Loading branch information...
commit 67a58801623cccd55ecdd7d21f7b2dc487175c03 1 parent 0995396
@jacobian jacobian authored
View
2  django/contrib/contenttypes/generic.py
@@ -253,6 +253,8 @@ def get_query_set(self):
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()
View
5 django/db/models/fields/related.py
@@ -325,6 +325,8 @@ def get_query_set(self):
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 _add_items(self, source_col_name, target_col_name, *objs):
# 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.
View
6 tests/modeltests/many_to_many/models.py
@@ -61,6 +61,12 @@ class Meta:
# Adding a second time is OK
>>> a2.publications.add(p3)
+# Adding an object of the wrong type raises TypeError
+>>> a2.publications.add(a1)
+Traceback (most recent call last):
+...
+TypeError: 'Publication' instance expected
+
# Add a Publication directly via publications.add by using keyword arguments.
>>> new_publication = a2.publications.create(title='Highlights for Children')
View
7 tests/modeltests/many_to_one/models.py
@@ -72,6 +72,13 @@ class Meta:
>>> r2.article_set.add(new_article2)
>>> new_article2.reporter.id
2
+
+# Adding an object of the wrong type raises TypeError
+>>> r.article_set.add(r2)
+Traceback (most recent call last):
+...
+TypeError: 'Article' instance expected
+
>>> r.article_set.all()
[<Article: John's second story>, <Article: This is a test>]
>>> r2.article_set.all()

0 comments on commit 67a5880

Please sign in to comment.
Something went wrong with that request. Please try again.