From 0efafa4c54ab8ad684df66c8e34928bb2ba88656 Mon Sep 17 00:00:00 2001
From: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Mon, 18 Mar 2013 11:09:29 +0100
Subject: [PATCH] Fixed #18447 -- Made LazyObject unwrap on dict access.

Thanks Roman Gladkov and Zbigniew Siciarz.
---
 django/utils/functional.py            | 15 ++++++++++++++-
 tests/utils_tests/simplelazyobject.py | 10 ++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/django/utils/functional.py b/django/utils/functional.py
index d740acf8f2..f19a46aa3a 100644
--- a/django/utils/functional.py
+++ b/django/utils/functional.py
@@ -249,9 +249,22 @@ class LazyObject(object):
         """
         raise NotImplementedError
 
-    # introspection support:
+    # Introspection support
     __dir__ = new_method_proxy(dir)
 
+    # Dictionary methods support
+    @new_method_proxy
+    def __getitem__(self, key):
+        return self[key]
+
+    @new_method_proxy
+    def __setitem__(self, key, value):
+        self[key] = value
+
+    @new_method_proxy
+    def __delitem__(self, key):
+        del self[key]
+
 
 # Workaround for http://bugs.python.org/issue12370
 _super = super
diff --git a/tests/utils_tests/simplelazyobject.py b/tests/utils_tests/simplelazyobject.py
index 2bc6799372..3bd6bf1e7f 100644
--- a/tests/utils_tests/simplelazyobject.py
+++ b/tests/utils_tests/simplelazyobject.py
@@ -128,3 +128,13 @@ class TestUtilsSimpleLazyObject(TestCase):
         self.assertEqual(unpickled, x)
         self.assertEqual(six.text_type(unpickled), six.text_type(x))
         self.assertEqual(unpickled.name, x.name)
+
+    def test_dict(self):
+        # See ticket #18447
+        lazydict = SimpleLazyObject(lambda: {'one': 1})
+        self.assertEqual(lazydict['one'], 1)
+        lazydict['one'] = -1
+        self.assertEqual(lazydict['one'], -1)
+        del lazydict['one']
+        with self.assertRaises(KeyError):
+            lazydict['one']