1
0
mirror of https://github.com/django/django.git synced 2025-10-26 07:06:08 +00:00

Fixed #21563 -- Single related object descriptors should work with hasattr.

Thanks to Aymeric Augustin for the review and Trac alias monkut for the report.
This commit is contained in:
Simon Charette
2013-12-08 14:12:01 -05:00
parent c7c647419c
commit 75924cfa6d
3 changed files with 45 additions and 7 deletions

View File

@@ -33,5 +33,14 @@ class ReverseSingleRelatedTests(TestCase):
# of the "bare" queryset. Usually you'd define this as a property on the class,
# but this approximates that in a way that's easier in tests.
Source.objects.use_for_related_fields = True
private_item = Item.objects.get(pk=private_item.pk)
self.assertRaises(Source.DoesNotExist, lambda: private_item.source)
try:
private_item = Item.objects.get(pk=private_item.pk)
self.assertRaises(Source.DoesNotExist, lambda: private_item.source)
finally:
Source.objects.use_for_related_fields = False
def test_hasattr_single_related(self):
# The exception raised on attribute access when a related object
# doesn't exist should be an instance of a subclass of `AttributeError`
# refs #21563
self.assertFalse(hasattr(Item(), 'source'))