Ticket #1745: m2m_requires_pk_val.patch

File m2m_requires_pk_val.patch, 1.3 KB (added by Luke Plant, 19 years ago)
  • django/db/models/fields/related.py

     
    239239            self.join_table = join_table
    240240            self.source_col_name = source_col_name
    241241            self.target_col_name = target_col_name
    242             if instance:
    243                 self._pk_val = self.instance._get_pk_val()
     242            self._pk_val = self.instance._get_pk_val()
     243            if self._pk_val is None:
     244                raise ValueError("%r instance needs to have a primary key value before a many-to-many relationship can be used." % model)
    244245
    245246        def get_query_set(self):
    246247            return superclass.get_query_set(self).filter(**(self.core_filters))
  • tests/modeltests/many_to_many/models.py

     
    110110>>> a2.delete()
    111111>>> Article.objects.all()
    112112[Django lets you build Web apps easily]
    113 >>> p1.article_set.all()
    114 [Django lets you build Web apps easily]
     113>>> p2.article_set.all()
     114[]
    115115
    116116# Adding via the 'other' end of an m2m
    117117>>> a4 = Article(headline='NASA finds intelligent life on Earth')
Back to Top