diff options
| author | Aljaž Košir <aljazkosir5@gmail.com> | 2021-09-30 12:07:50 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-09-30 12:09:24 +0200 |
| commit | 0a49276065575867fbbf279f138b399c535e3b6d (patch) | |
| tree | a6f89ad632a2f40f19f4232b1d9c1d38227db4d3 | |
| parent | 93a42d43a6995993b9bbcb743ab3c2a2b8414ebd (diff) | |
[4.0.x] Fixed #33155 -- Made ModelChoiceIteratorValue instances hashable.
Backport of 7b8beeee3d45cafd7bec7ff3ee0e4371e142c36d from main
| -rw-r--r-- | AUTHORS | 1 | ||||
| -rw-r--r-- | django/forms/models.py | 3 | ||||
| -rw-r--r-- | tests/model_forms/test_modelchoicefield.py | 8 |
3 files changed, 11 insertions, 1 deletions
@@ -54,6 +54,7 @@ answer newbie questions, and generally made Django that much better: Alexey Boriskin <alex@boriskin.me> Alexey Tsivunin <most-208@yandex.ru> Ali Vakilzade <ali@vakilzade.com> + Aljaž Košir <aljazkosir5@gmail.com> Aljosa Mohorovic <aljosa.mohorovic@gmail.com> Amit Chakradeo <https://amit.chakradeo.net/> Amit Ramon <amit.ramon@gmail.com> diff --git a/django/forms/models.py b/django/forms/models.py index 5dcf923c12..7effb202e3 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -1166,6 +1166,9 @@ class ModelChoiceIteratorValue: def __str__(self): return str(self.value) + def __hash__(self): + return hash(self.value) + def __eq__(self, other): if isinstance(other, ModelChoiceIteratorValue): other = other.value diff --git a/tests/model_forms/test_modelchoicefield.py b/tests/model_forms/test_modelchoicefield.py index 2a0c05d803..30d1a05821 100644 --- a/tests/model_forms/test_modelchoicefield.py +++ b/tests/model_forms/test_modelchoicefield.py @@ -2,7 +2,7 @@ import datetime from django import forms from django.core.exceptions import ValidationError -from django.forms.models import ModelChoiceIterator +from django.forms.models import ModelChoiceIterator, ModelChoiceIteratorValue from django.forms.widgets import CheckboxSelectMultiple from django.template import Context, Template from django.test import TestCase @@ -341,6 +341,12 @@ class ModelChoiceFieldTests(TestCase): </div>""" % (self.c1.pk, self.c2.pk, self.c3.pk), ) + def test_choice_value_hash(self): + value_1 = ModelChoiceIteratorValue(self.c1.pk, self.c1) + value_2 = ModelChoiceIteratorValue(self.c2.pk, self.c2) + self.assertEqual(hash(value_1), hash(ModelChoiceIteratorValue(self.c1.pk, None))) + self.assertNotEqual(hash(value_1), hash(value_2)) + def test_choices_not_fetched_when_not_rendering(self): with self.assertNumQueries(1): field = forms.ModelChoiceField(Category.objects.order_by('-name')) |
