summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Johnson <me@adamj.eu>2022-01-13 18:51:18 +0000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-01-14 07:00:48 +0100
commitfdfa97fb166ef5065aa2b229f19cb4ce303084e5 (patch)
tree64850839d093016c3838638640c9774345fc937c
parent652c68ffeebd510a6f59e1b56b3e007d07683ad8 (diff)
Fixed #33441 -- Restored immutability of models.Field.__hash__().
Regression in 502e75f9ed5476ffe8229109acf0c23999d4b533.
-rw-r--r--django/db/models/fields/__init__.py6
-rw-r--r--tests/model_fields/tests.py11
2 files changed, 9 insertions, 8 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 88c9ca9f28..fcea1597ef 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -542,11 +542,7 @@ class Field(RegisterLookupMixin):
return NotImplemented
def __hash__(self):
- return hash((
- self.creation_counter,
- self.model._meta.app_label if hasattr(self, 'model') else None,
- self.model._meta.model_name if hasattr(self, 'model') else None,
- ))
+ return hash(self.creation_counter)
def __deepcopy__(self, memodict):
# We don't have to deepcopy very much here, since most things are not
diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py
index 5208b40dc9..86365989f0 100644
--- a/tests/model_fields/tests.py
+++ b/tests/model_fields/tests.py
@@ -128,9 +128,14 @@ class BasicFieldTests(SimpleTestCase):
self.assertLess(abstract_model_field, inherit2_model_field)
self.assertLess(inherit1_model_field, inherit2_model_field)
- self.assertNotEqual(hash(abstract_model_field), hash(inherit1_model_field))
- self.assertNotEqual(hash(abstract_model_field), hash(inherit2_model_field))
- self.assertNotEqual(hash(inherit1_model_field), hash(inherit2_model_field))
+ def test_hash_immutability(self):
+ field = models.IntegerField()
+ field_hash = hash(field)
+
+ class MyModel(models.Model):
+ rank = field
+
+ self.assertEqual(field_hash, hash(field))
class ChoicesTests(SimpleTestCase):