Author: jacob Date: 2009-03-31 16:42:47 -0500 (Tue, 31 Mar 2009) New Revision: 10293
Modified: django/branches/releases/1.0.X/ django/branches/releases/1.0.X/django/contrib/contenttypes/generic.py django/branches/releases/1.0.X/django/db/models/fields/related.py django/branches/releases/1.0.X/tests/modeltests/many_to_many/models.py django/branches/releases/1.0.X/tests/modeltests/many_to_one/models.py Log: [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. Property changes on: django/branches/releases/1.0.X ___________________________________________________________________ Name: svnmerge-integrated - /django/trunk:1-9097,9099-9102,9104-9109,9111,9113-9144,9146-9151,9153-9156,9158-9159,9161-9187,9189-9247,9249-9262,9264-9277,9279-9298,9301-9302,9305-9331,9333-9343,9345,9347,9350-9352,9355-9396,9399-9462,9466-9469,9471-9488,9491-9526,9529,9533-9536,9539-9550,9556-9557,9559-9560,9562-9568,9570-9591,9595-9619,9621-9624,9626-9636,9638-9642,9644-9645,9647-9689,9691-9699,9703-9706,9709-9713,9716-9723,9725-9726,9730-9738,9740-9741,9750-9751,9757-9758,9761-9762,9767-9768,9770-9780,9782-9784,9789-9790,9793-9798,9801-9802,9806-9807,9809-9813,9821-9837,9842-9843,9847-9859,9861,9863-9875,9877-9881,9883-9887,9899-9903,9906-9909,9912,9914,9916-9917,9919-9920,9922-9927,9929,9931-9937,9939,9942-9943,9945-9950,9953-9954,9956-9962,9966-9977,9979-9984,9986-9988,9990,9992,9994,9996-9998,10003,10007,10009,10013,10015,10017,10022,10024,10031,10036-10037,10040,10049,10051,10054,10056-10058,10071,10073-10074,10078,10104,10125,10136,10139,10143,10145-10147,10149-10152,10170,10175,10193,10195-10196,10198-10221,10223-10225,10230-10232,10236-10237,10239-10240 + /django/trunk:1-9097,9099-9102,9104-9109,9111,9113-9144,9146-9151,9153-9156,9158-9159,9161-9187,9189-9247,9249-9262,9264-9277,9279-9298,9301-9302,9305-9331,9333-9343,9345,9347,9350-9352,9355-9396,9399-9462,9466-9469,9471-9488,9491-9526,9529,9533-9536,9539-9550,9556-9557,9559-9560,9562-9568,9570-9591,9595-9619,9621-9624,9626-9636,9638-9642,9644-9645,9647-9689,9691-9699,9703-9706,9709-9713,9716-9723,9725-9726,9730-9738,9740-9741,9750-9751,9757-9758,9761-9762,9767-9768,9770-9780,9782-9784,9789-9790,9793-9798,9801-9802,9806-9807,9809-9813,9821-9837,9842-9843,9847-9859,9861,9863-9875,9877-9881,9883-9887,9899-9903,9906-9909,9912,9914,9916-9917,9919-9920,9922-9927,9929,9931-9937,9939,9942-9943,9945-9950,9953-9954,9956-9962,9966-9977,9979-9984,9986-9988,9990,9992,9994,9996-9998,10003,10007,10009,10013,10015,10017,10022,10024,10031,10036-10037,10040,10049,10051,10054,10056-10058,10071,10073-10074,10078,10104,10125,10136,10139,10143,10145-10147,10149-10152,10170,10175,10193,10195-10196,10198-10221,10223-10226,10230-10232,10236-10237,10239-10240 Modified: django/branches/releases/1.0.X/django/contrib/contenttypes/generic.py =================================================================== --- django/branches/releases/1.0.X/django/contrib/contenttypes/generic.py 2009-03-31 21:40:40 UTC (rev 10292) +++ django/branches/releases/1.0.X/django/contrib/contenttypes/generic.py 2009-03-31 21:42:47 UTC (rev 10293) @@ -253,6 +253,8 @@ 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() Modified: django/branches/releases/1.0.X/django/db/models/fields/related.py =================================================================== --- django/branches/releases/1.0.X/django/db/models/fields/related.py 2009-03-31 21:40:40 UTC (rev 10292) +++ django/branches/releases/1.0.X/django/db/models/fields/related.py 2009-03-31 21:42:47 UTC (rev 10293) @@ -325,6 +325,8 @@ 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 @@ # 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. Modified: django/branches/releases/1.0.X/tests/modeltests/many_to_many/models.py =================================================================== --- django/branches/releases/1.0.X/tests/modeltests/many_to_many/models.py 2009-03-31 21:40:40 UTC (rev 10292) +++ django/branches/releases/1.0.X/tests/modeltests/many_to_many/models.py 2009-03-31 21:42:47 UTC (rev 10293) @@ -61,6 +61,12 @@ # 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') Modified: django/branches/releases/1.0.X/tests/modeltests/many_to_one/models.py =================================================================== --- django/branches/releases/1.0.X/tests/modeltests/many_to_one/models.py 2009-03-31 21:40:40 UTC (rev 10292) +++ django/branches/releases/1.0.X/tests/modeltests/many_to_one/models.py 2009-03-31 21:42:47 UTC (rev 10293) @@ -72,6 +72,13 @@ >>> 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() --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-updates?hl=en -~----------~----~----~----~------~----~------~--~---