commit 5581baf0c79322fc2449b685859cd46666cae390
Author: Jordan Rhys Powell <jordan.rhys.powell@gmail.com>
Date: Sun Mar 24 23:10:39 2013 +0000
Fixed #17582 - Improve foreign field DoesNotExist message
Added a message when accessing a foreign field fails due to a null or
invalid value. The message includes the model containing the field and
the field name.
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index b575583..7ca701a 100644
a
|
b
|
class ReverseSingleRelatedObjectDescriptor(six.with_metaclass(RenameRelatedObjec
|
307 | 307 | setattr(rel_obj, self.field.related.get_cache_name(), instance) |
308 | 308 | setattr(instance, self.cache_name, rel_obj) |
309 | 309 | if rel_obj is None and not self.field.null: |
310 | | raise self.field.rel.to.DoesNotExist |
| 310 | raise self.field.rel.to.DoesNotExist( |
| 311 | "%s has no %s." % (self.field.model.__name__, self.field.name)) |
311 | 312 | else: |
312 | 313 | return rel_obj |
313 | 314 | |
diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt
index 236be7b..8923cd2 100644
a
|
b
|
Minor features
|
181 | 181 | and the undocumented limit of the higher of 1000 or ``max_num`` forms |
182 | 182 | was changed so it is always 1000 more than ``max_num``. |
183 | 183 | |
| 184 | * The ``DoesNotExist`` exception message now indicates which foreign field is null or invalid |
| 185 | |
184 | 186 | Backwards incompatible changes in 1.6 |
185 | 187 | ===================================== |
186 | 188 | |
diff --git a/tests/foreign_object/tests.py b/tests/foreign_object/tests.py
index 2ca13cb..240f2a6 100644
a
|
b
|
class MultiColumnFKTests(TestCase):
|
316 | 316 | list(Article.objects.filter(active_translation__abstract=None)), |
317 | 317 | [a1, a2]) |
318 | 318 | |
| 319 | def test_foreign_key_raises_informative_does_not_exist(self): |
| 320 | referrer = ArticleTranslation() |
| 321 | with self.assertRaisesMessage(Article.DoesNotExist, 'ArticleTranslation has no article'): |
| 322 | referrer.article |
| 323 | |
319 | 324 | class FormsTests(TestCase): |
320 | 325 | # ForeignObjects should not have any form fields, currently the user needs |
321 | 326 | # to manually deal with the foreignobject relation. |