Ticket #10413: 10413_2.diff

File 10413_2.diff, 3.1 KB (added by David Gouldin, 16 years ago)
  • django/db/models/fields/related.py

     
    332332
    333333            def add(self, *objs):
    334334                for obj in objs:
     335                    if not isinstance(obj, self.model):
     336                        raise TypeError, "'%s' instance expected" % self.model._meta.object_name
    335337                    setattr(obj, rel_field.name, instance)
    336338                    obj.save()
    337339            add.alters_data = True
     
    452454
    453455            # If there aren't any objects, there is nothing to do.
    454456            if objs:
     457                from django.db.models.base import Model
    455458                # Check that all the objects are of the right type
    456459                new_ids = set()
    457460                for obj in objs:
    458461                    if isinstance(obj, self.model):
    459462                        new_ids.add(obj._get_pk_val())
     463                    elif isinstance(obj, Model):
     464                        raise TypeError, "'%s' instance expected" % self.model._meta.object_name
    460465                    else:
    461466                        new_ids.add(obj)
    462467                # Add the newly created or already existing objects to the join table.
  • django/contrib/contenttypes/generic.py

     
    253253
    254254        def add(self, *objs):
    255255            for obj in objs:
     256                if not isinstance(obj, self.model):
     257                    raise TypeError, "'%s' instance expected" % self.model._meta.object_name
    256258                setattr(obj, self.content_type_field_name, self.content_type)
    257259                setattr(obj, self.object_id_field_name, self.pk_val)
    258260                obj.save()
  • tests/modeltests/many_to_many/models.py

     
    6161# Adding a second time is OK
    6262>>> a2.publications.add(p3)
    6363
     64# Adding an object of the wrong type raises TypeError
     65>>> a2.publications.add(a1)
     66Traceback (most recent call last):
     67...
     68TypeError: 'Publication' instance expected
     69
    6470# Add a Publication directly via publications.add by using keyword arguments.
    6571>>> new_publication = a2.publications.create(title='Highlights for Children')
    6672
  • tests/modeltests/many_to_one/models.py

     
    7272>>> r2.article_set.add(new_article2)
    7373>>> new_article2.reporter.id
    74742
     75
     76# Adding an object of the wrong type raises TypeError
     77>>> r.article_set.add(r2)
     78Traceback (most recent call last):
     79...
     80TypeError: 'Article' instance expected
     81
    7582>>> r.article_set.all()
    7683[<Article: John's second story>, <Article: This is a test>]
    7784>>> r2.article_set.all()
Back to Top