diff --git a/django/utils/tree.py b/django/utils/tree.py index 392838b4820..b7f7b9798b8 100644 --- a/django/utils/tree.py +++ b/django/utils/tree.py @@ -70,6 +70,9 @@ class Node: self.children == other.children ) + def __hash__(self): + return hash((self.__class__, self.connector, self.negated) + tuple(self.children)) + def add(self, data, conn_type, squash=True): """ Combine this tree and the data represented by data using the diff --git a/tests/utils_tests/test_tree.py b/tests/utils_tests/test_tree.py index 98db5f6012f..65f49c06a6c 100644 --- a/tests/utils_tests/test_tree.py +++ b/tests/utils_tests/test_tree.py @@ -19,6 +19,16 @@ class NodeTests(unittest.TestCase): "") self.assertEqual(repr(self.node2), "") + def test_hash(self): + node3 = Node(self.node1_children, negated=True) + node4 = Node(self.node1_children, connector='OTHER') + node5 = Node(self.node1_children) + self.assertNotEqual(hash(self.node1), hash(self.node2)) + self.assertNotEqual(hash(self.node1), hash(node3)) + self.assertNotEqual(hash(self.node1), hash(node4)) + self.assertEqual(hash(self.node1), hash(node5)) + self.assertEqual(hash(self.node2), hash(Node())) + def test_len(self): self.assertEqual(len(self.node1), 2) self.assertEqual(len(self.node2), 0)