Ticket #10413: 10413.diff

File 10413.diff, 3.3 KB (added by David Gouldin, 15 years ago)
  • django/contrib/contenttypes/generic.py

    a b def create_generic_related_manager(superclass):  
    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()
  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    index 146a1ae..af88959 100644
    a b RECURSIVE_RELATIONSHIP_CONSTANT = 'self'  
    2020
    2121pending_lookups = {}
    2222
     23_Model = None
     24def get_model_class():
     25    global _Model
     26    if _Model is None:
     27        from django.db.models.base import Model
     28        _Model = Model
     29    return _Model
     30
    2331def add_lazy_relation(cls, field, relation, operation):
    2432    """
    2533    Adds a lookup on ``cls`` when a related field is defined using a string,
    class ForeignRelatedObjectsDescriptor(object):  
    302310
    303311            def add(self, *objs):
    304312                for obj in objs:
     313                    if not isinstance(obj, self.model):
     314                        raise TypeError, "'%s' instance expected" % self.model._meta.object_name
    305315                    setattr(obj, rel_field.name, instance)
    306316                    obj.save()
    307317            add.alters_data = True
    def create_many_related_manager(superclass, through=False):  
    438448                for obj in objs:
    439449                    if isinstance(obj, self.model):
    440450                        new_ids.add(obj._get_pk_val())
     451                    elif isinstance(obj, get_model_class()):
     452                        raise TypeError, "'%s' instance expected" % self.model._meta.object_name
    441453                    else:
    442454                        new_ids.add(obj)
    443455                # Add the newly created or already existing objects to the join table.
  • tests/modeltests/many_to_many/models.py

    diff --git a/tests/modeltests/many_to_many/models.py b/tests/modeltests/many_to_many/models.py
    index cc34c86..21b4e82 100644
    a b ValueError: 'Article' instance needs to have a primary key value before a many-t  
    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

    diff --git a/tests/modeltests/many_to_one/models.py b/tests/modeltests/many_to_one/models.py
    index 1972ac7..8093e73 100644
    a b __test__ = {'API_TESTS':"""  
    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