Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Fixed #1745 -- Accessing many-to-many relationships without a PK valu…

…e now throws an exception. Thanks, Luke

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent aba95b7 commit 69d6bd7af380d47edee2c7bdf02fef41e183acd1 @adrianholovaty adrianholovaty committed
5 django/db/models/fields/
@@ -239,8 +239,9 @@ def __init__(self, model=None, core_filters=None, instance=None, symmetrical=Non
self.join_table = join_table
self.source_col_name = source_col_name
self.target_col_name = target_col_name
- if instance:
- self._pk_val = self.instance._get_pk_val()
+ 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)
def get_query_set(self):
return superclass.get_query_set(self).filter(**(self.core_filters))
4 tests/modeltests/many_to_many/
@@ -110,8 +110,8 @@ class Meta:
>>> a2.delete()
>>> Article.objects.all()
[Django lets you build Web apps easily]
->>> p1.article_set.all()
-[Django lets you build Web apps easily]
+>>> p2.article_set.all()
# Adding via the 'other' end of an m2m
>>> a4 = Article(headline='NASA finds intelligent life on Earth')

0 comments on commit 69d6bd7

Please sign in to comment.
Something went wrong with that request. Please try again.