Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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
Jacob Kaplan-Moss authored March 31, 2009
2  django/contrib/contenttypes/generic.py
@@ -253,6 +253,8 @@ def get_query_set(self):
253 253
 
254 254
         def add(self, *objs):
255 255
             for obj in objs:
  256
+                if not isinstance(obj, self.model):
  257
+                    raise TypeError, "'%s' instance expected" % self.model._meta.object_name
256 258
                 setattr(obj, self.content_type_field_name, self.content_type)
257 259
                 setattr(obj, self.object_id_field_name, self.pk_val)
258 260
                 obj.save()
5  django/db/models/fields/related.py
@@ -325,6 +325,8 @@ def get_query_set(self):
@@ -445,11 +447,14 @@ def _add_items(self, source_col_name, target_col_name, *objs):
6  tests/modeltests/many_to_many/models.py
@@ -61,6 +61,12 @@ class Meta:
61 61
 # Adding a second time is OK
62 62
 >>> a2.publications.add(p3)
63 63
 
  64
+# Adding an object of the wrong type raises TypeError
  65
+>>> a2.publications.add(a1)
  66
+Traceback (most recent call last):
  67
+...
  68
+TypeError: 'Publication' instance expected
  69
+
64 70
 # Add a Publication directly via publications.add by using keyword arguments.
65 71
 >>> new_publication = a2.publications.create(title='Highlights for Children')
66 72
 
7  tests/modeltests/many_to_one/models.py
@@ -72,6 +72,13 @@ class Meta:
72 72
 >>> r2.article_set.add(new_article2)
73 73
 >>> new_article2.reporter.id
74 74
 2
  75
+
  76
+# Adding an object of the wrong type raises TypeError
  77
+>>> r.article_set.add(r2)
  78
+Traceback (most recent call last):
  79
+...
  80
+TypeError: 'Article' instance expected
  81
+
75 82
 >>> r.article_set.all()
76 83
 [<Article: John's second story>, <Article: This is a test>]
77 84
 >>> r2.article_set.all()

0 notes on commit 67a5880

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