Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17527 -- Improved exception message when adding the wrong type…

… of object to a ManyToManyField. Thanks, guettli and cClaude Paroz.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17437 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2b0c1ea64155bd3f825b06e266d73c00dbf4196f 1 parent 56d787d
Jannis Leidel authored
4  django/db/models/fields/related.py
@@ -474,7 +474,7 @@ def get_prefetch_query_set(self, instances):
@@ -636,7 +636,7 @@ def _add_items(self, source_field_name, target_field_name, *objs):
5  tests/modeltests/many_to_many/tests.py
... ...
@@ -1,4 +1,4 @@
1  
-from __future__ import absolute_import
  1
+from __future__ import absolute_import, with_statement
2 2
 
3 3
 from django.test import TestCase
4 4
 
@@ -52,7 +52,8 @@ def test_add(self):
52 52
             ])
53 53
 
54 54
         # Adding an object of the wrong type raises TypeError
55  
-        self.assertRaises(TypeError, a6.publications.add, a5)
  55
+        with self.assertRaisesRegexp(TypeError, "'Publication' instance expected, got <Article.*"):
  56
+            a6.publications.add(a5)
56 57
         # Add a Publication directly via publications.add by using keyword arguments.
57 58
         p4 = a6.publications.create(title='Highlights for Adults')
58 59
         self.assertQuerysetEqual(a6.publications.all(),
5  tests/modeltests/many_to_one/tests.py
... ...
@@ -1,4 +1,4 @@
1  
-from __future__ import absolute_import
  1
+from __future__ import absolute_import, with_statement
2 2
 
3 3
 from copy import deepcopy
4 4
 from datetime import datetime
@@ -68,7 +68,8 @@ def test_add(self):
68 68
         self.assertQuerysetEqual(self.r2.article_set.all(), ["<Article: Paul's story>"])
69 69
 
70 70
         # Adding an object of the wrong type raises TypeError.
71  
-        self.assertRaises(TypeError, self.r.article_set.add, self.r2)
  71
+        with self.assertRaisesRegexp(TypeError, "'Article' instance expected, got <Reporter.*"):
  72
+            self.r.article_set.add(self.r2)
72 73
         self.assertQuerysetEqual(self.r.article_set.all(),
73 74
             [
74 75
                 "<Article: John's second story>",

0 notes on commit 2b0c1ea

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