Ticket #17527: 17527-2.diff

File 17527-2.diff, 3.1 KB (added by claudep, 4 years ago)

Patch with tests

  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 848fd6e..53f9b9f 100644
    a b class ForeignRelatedObjectsDescriptor(object): 
    474474            def add(self, *objs):
    475475                for obj in objs:
    476476                    if not isinstance(obj, self.model):
    477                         raise TypeError("'%s' instance expected" % self.model._meta.object_name)
     477                        raise TypeError("'%s' instance expected, got %r" % (self.model._meta.object_name, obj))
    478478                    setattr(obj, rel_field.name, self.instance)
    479479                    obj.save()
    480480            add.alters_data = True
    def create_many_related_manager(superclass, rel): 
    636636                                               (obj, self.instance._state.db, obj._state.db))
    637637                        new_ids.add(obj.pk)
    638638                    elif isinstance(obj, Model):
    639                         raise TypeError("'%s' instance expected" % self.model._meta.object_name)
     639                        raise TypeError("'%s' instance expected, got %r" % (self.model._meta.object_name, obj))
    640640                    else:
    641641                        new_ids.add(obj)
    642642                db = router.db_for_write(self.through, instance=self.instance)
  • tests/modeltests/many_to_many/tests.py

    diff --git a/tests/modeltests/many_to_many/tests.py b/tests/modeltests/many_to_many/tests.py
    index b00d7da..9fa524f 100644
    a b  
    1 from __future__ import absolute_import
     1from __future__ import absolute_import, with_statement
    22
    33from django.test import TestCase
    44
    class ManyToManyTests(TestCase): 
    5252            ])
    5353
    5454        # 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)
    5657        # Add a Publication directly via publications.add by using keyword arguments.
    5758        p4 = a6.publications.create(title='Highlights for Adults')
    5859        self.assertQuerysetEqual(a6.publications.all(),
  • tests/modeltests/many_to_one/tests.py

    diff --git a/tests/modeltests/many_to_one/tests.py b/tests/modeltests/many_to_one/tests.py
    index 922506e..bc9fe64 100644
    a b  
    1 from __future__ import absolute_import
     1from __future__ import absolute_import, with_statement
    22
    33from copy import deepcopy
    44from datetime import datetime
    class ManyToOneTests(TestCase): 
    6868        self.assertQuerysetEqual(self.r2.article_set.all(), ["<Article: Paul's story>"])
    6969
    7070        # 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)
    7273        self.assertQuerysetEqual(self.r.article_set.all(),
    7374            [
    7475                "<Article: John's second story>",
Back to Top