diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 4ea0c3fb06..41d625186c 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -351,7 +351,7 @@ def create_many_related_manager(superclass): self.target_col_name = target_col_name self._pk_val = self.instance._get_pk_val() if self._pk_val is None: - raise ValueError("%r instance needs to have a primary key value before a many-to-many relationship can be used." % model) + raise ValueError("%r instance needs to have a primary key value before a many-to-many relationship can be used." % instance.__class__.__name__) def get_query_set(self): return superclass.get_query_set(self).filter(**(self.core_filters)) diff --git a/tests/modeltests/many_to_many/models.py b/tests/modeltests/many_to_many/models.py index e09fd825f8..c2ab2897b6 100644 --- a/tests/modeltests/many_to_many/models.py +++ b/tests/modeltests/many_to_many/models.py @@ -39,6 +39,14 @@ __test__ = {'API_TESTS':""" # Create an Article. >>> a1 = Article(id=None, headline='Django lets you build Web apps easily') + +# You can't associate it with a Publication until it's been saved. +>>> a1.publications.add(p1) +Traceback (most recent call last): +... +ValueError: 'Article' instance needs to have a primary key value before a many-to-many relationship can be used. + +# Save it! >>> a1.save() # Associate the Article with a Publication.