Skip to content

Commit

Permalink
Fixed #7026 -- Fixed misleading/incorrect exception text when adding …
Browse files Browse the repository at this point in the history
…to a many-to-many set on an object that doesn't yet have a primary-key value. Thanks for the report, ryan@peaceworks.ca

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7622 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrianholovaty committed Jun 12, 2008
1 parent 0fd41d6 commit 5309e18
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/db/models/fields/related.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def __init__(self, model=None, core_filters=None, instance=None, symmetrical=Non
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))
Expand Down
8 changes: 8 additions & 0 deletions tests/modeltests/many_to_many/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class Meta:
# 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.
Expand Down

0 comments on commit 5309e18

Please sign in to comment.