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'
|
20 | 20 | |
21 | 21 | pending_lookups = {} |
22 | 22 | |
| 23 | _Model = None |
| 24 | def 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 | |
23 | 31 | def add_lazy_relation(cls, field, relation, operation): |
24 | 32 | """ |
25 | 33 | Adds a lookup on ``cls`` when a related field is defined using a string, |
… |
… |
class ForeignRelatedObjectsDescriptor(object):
|
302 | 310 | |
303 | 311 | def add(self, *objs): |
304 | 312 | for obj in objs: |
| 313 | if not isinstance(obj, self.model): |
| 314 | raise TypeError, "'%s' instance expected" % self.model._meta.object_name |
305 | 315 | setattr(obj, rel_field.name, instance) |
306 | 316 | obj.save() |
307 | 317 | add.alters_data = True |
… |
… |
def create_many_related_manager(superclass, through=False):
|
438 | 448 | for obj in objs: |
439 | 449 | if isinstance(obj, self.model): |
440 | 450 | 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 |
441 | 453 | else: |
442 | 454 | new_ids.add(obj) |
443 | 455 | # Add the newly created or already existing objects to the join table. |
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
|
61 | 61 | # Adding a second time is OK |
62 | 62 | >>> a2.publications.add(p3) |
63 | 63 | |
| 64 | # Adding an object of the wrong type raises TypeError |
| 65 | >>> a2.publications.add(a1) |
| 66 | Traceback (most recent call last): |
| 67 | ... |
| 68 | TypeError: 'Publication' instance expected |
| 69 | |
64 | 70 | # Add a Publication directly via publications.add by using keyword arguments. |
65 | 71 | >>> new_publication = a2.publications.create(title='Highlights for Children') |
66 | 72 | |
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':"""
|
72 | 72 | >>> r2.article_set.add(new_article2) |
73 | 73 | >>> new_article2.reporter.id |
74 | 74 | 2 |
| 75 | |
| 76 | # Adding an object of the wrong type raises TypeError |
| 77 | >>> r.article_set.add(r2) |
| 78 | Traceback (most recent call last): |
| 79 | ... |
| 80 | TypeError: 'Article' instance expected |
| 81 | |
75 | 82 | >>> r.article_set.all() |
76 | 83 | [<Article: John's second story>, <Article: This is a test>] |
77 | 84 | >>> r2.article_set.all() |